Created
December 13, 2016 20:03
-
-
Save joshfeck/0e10424fff36bd62bcd72a4c76cd0687 to your computer and use it in GitHub Desktop.
Simplified version of https://github.com/eventespresso/ee-code-snippet-library/blob/master/checkout/bc_ee_apply_transaction_surcharge.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//* Please do NOT include the opening php tag, except of course if you're starting with a blank file | |
/** | |
* DO NOT EDIT ANYTHING EXCEPT DEFAULT SURCHARGE DETAILS | |
* | |
* bc_ee_apply_transaction_surcharge | |
* | |
* @param \EE_Checkout $checkout | |
* @return \EE_Checkout | |
*/ | |
function bc_ee_apply_transaction_surcharge( EE_Checkout $checkout ) { | |
// DEFAULT SURCHARGE DETAILS - EDIT THIS | |
$surcharge_details = | |
array( | |
// name for surcharge that will be displayed, ie: 'printing fee' | |
'name' => 'shipping fee', | |
// unique code used to identify surcharge in the db, ie: 'print-at-home-fee' | |
'code' => 'ticket-shipping-fee', | |
// 'fee for printing tickets' | |
'description' => 'postal fee for shipping tickets', | |
// surcharge amount | |
'unit_price' => 2.50, | |
// whether or not tax is applied on top of the surcharge | |
'taxable' => false, | |
); | |
// 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; | |
} | |
// has surcharge already been applied ? | |
$existing_surcharge = $grand_total->get_child_line_item( $surcharge_details[ 'code' ] ); | |
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 ); | |
$pre_tax_subtotal->add_child_line_item( | |
EE_Line_Item::new_instance( array( | |
'LIN_name' => $surcharge_details[ 'name' ], | |
'LIN_desc' => $surcharge_details[ 'description' ], | |
'LIN_unit_price' => (float) $surcharge_details[ 'unit_price' ], | |
'LIN_quantity' => 1, | |
'LIN_is_taxable' => $surcharge_details[ 'taxable' ], | |
'LIN_order' => 0, | |
'LIN_total' => (float) $surcharge_details[ 'unit_price' ], | |
'LIN_type' => EEM_Line_Item::type_line_item, | |
'LIN_code' => $surcharge_details[ 'code' ], | |
) ) | |
); | |
$grand_total->recalculate_total_including_taxes(); | |
return $checkout; | |
} | |
add_filter( 'FHEE__EED_Single_Page_Checkout___initialize_checkout__checkout', 'bc_ee_apply_transaction_surcharge' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment