Amdur Productions
April 25, 2018 at 6:49 pm
Based on this post , I wrote this snippet to add a 4% surcharge to all orders (which are events + price modifier fields).
The weird issue is that the surcharge is always about $.50 higher than subtotal * .04. I’m not sure what’s causing this, but I feel like the surcharge might be getting added in once and then a percentage of that total is being taken?
function cortex_ee_apply_transaction_surcharge( EE_Checkout $checkout ) {
// verify checkout
if ( ! $checkout instanceof EE_Checkout ) {
return $checkout;
}
// verify cart
$cart = $checkout->cart;
if ( ! $cart instanceof EE_Cart ) {
return $checkout;
}
// verify grand total line item
$grand_total = $cart->get_grand_total();
if ( ! $grand_total instanceof EE_Line_Item ) {
return $checkout;
}
EE_Registry::instance()->load_helper( 'Line_Item' );
$pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal( $grand_total );
$existing_surcharge = $grand_total->get_child_line_item( 'cc-surcharge' );
if ( $existing_surcharge instanceof EE_Line_Item ) {
return $checkout;
}
$total = $grand_total->total();
$surcharge = $total * .04;
$pre_tax_subtotal->add_child_line_item(
EE_Line_Item::new_instance( array(
'LIN_name' => '4% Surcharge',
'LIN_desc' => '',
'LIN_unit_price' => (float)$surcharge,
'LIN_quantity' => 1,
'LIN_is_taxable' => false,
'LIN_order' => 0,
'LIN_total' => (float)$surcharge,
'LIN_type' => EEM_Line_Item::type_line_item,
'LIN_code' => 'cc-surcharge',
) )
);
$grand_total->recalculate_total_including_taxes();
return $checkout;
}
add_filter( 'FHEE__EED_Single_Page_Checkout___initialize_checkout__checkout', 'cortex_ee_apply_transaction_surcharge' );
Any insights?
Josh
April 26, 2018 at 6:43 am
Add New Note to this Reply
The problem with your code is nowhere does it set a line item percentage amount. You can have a look at this method to see an example of how to add a percentage based line item:
https://github.com/eventespresso/event-espresso-core/blob/master/core/helpers/EEH_Line_Item.helper.php#L89
You’ll note that the LIN_unit_price
is 0, the percentage amount is passed to LIN_percent
, and the calculation happens in LIN_total
.
Amdur Productions
April 26, 2018 at 2:58 pm
Add New Note to this Reply
Thanks! This works:
function cortex_ee_apply_transaction_surcharge( EE_Checkout $checkout ) {
// verify checkout
if ( ! $checkout instanceof EE_Checkout ) {
return $checkout;
}
// verify cart
$cart = $checkout->cart;
if ( ! $cart instanceof EE_Cart ) {
return $checkout;
}
// verify grand total line item
$grand_total = $cart->get_grand_total();
if ( ! $grand_total instanceof EE_Line_Item ) {
return $checkout;
}
// Make Sure Surcharge Only Gets applied once
$existing_surcharge = $grand_total->get_child_line_item( 'cc-surcharge' );
if ( $existing_surcharge instanceof EE_Line_Item ) {
return $checkout;
}
EE_Registry::instance()->load_helper( 'Line_Item' );
$pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal( $grand_total );
$percentage_amount = 4;
$pre_tax_subtotal->add_child_line_item(
EE_Line_Item::new_instance( array(
'LIN_name' => '4% Surcharge',
'LIN_desc' => '',
'LIN_unit_price' => 0,
'LIN_percent' => $percentage_amount,
'LIN_quantity' => 1,
'LIN_is_taxable' => false,
'LIN_total' => (float)($percentage_amount * ($grand_total->total() / 100)),
'LIN_type' => EEM_Line_Item::type_line_item,
'LIN_parent' => $grand_total->ID(),
'LIN_code' => 'cc-surcharge'
) )
);
$grand_total->recalculate_total_including_taxes();
return $checkout;
}
add_filter( 'FHEE__EED_Single_Page_Checkout___initialize_checkout__checkout', 'cortex_ee_apply_transaction_surcharge' );