Skip to content

Instantly share code, notes, and snippets.

@Pebblo
Forked from g000m/bc_add_cart_modifier.php
Last active January 12, 2022 20:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pebblo/54bde348a4981e3ea15575bc366c1b5f to your computer and use it in GitHub Desktop.
Save Pebblo/54bde348a4981e3ea15575bc366c1b5f to your computer and use it in GitHub Desktop.
<?php
/**
* bc_add_cart_modifier
*
* @param \EE_SPCO_Reg_Step $payment_options_reg_step
* @throws \EE_Error
*/
function bc_add_cart_modifier( EE_SPCO_Reg_Step $payment_options_reg_step ) {
// CHANGE THESE TO YOUR LIKING
$cart_modifier_name = 'my cart % surcharge';
$cart_modifier_amount = 10.00;
$cart_modifier_description = '10% surcharge for choosing invoice payment method';
$cart_modifier_taxable = false; // or false if surcharge is not taxable
$payment_methods_with_surcharges = array( 'square' );
// get what the user selected for payment method
$selected_method_of_payment = $payment_options_reg_step->checkout->selected_method_of_payment;
$cart = $payment_options_reg_step->checkout->cart;
if ( ! $cart instanceof EE_Cart ) {
// ERROR
return;
}
$total_line_item = $cart->get_grand_total();
if ( ! $total_line_item instanceof EE_Line_Item && ! $total_line_item->is_total() ) {
// ERROR
return;
}
$transaction = $payment_options_reg_step->checkout->transaction;
if ( ! $transaction instanceof EE_Transaction) {
// ERROR
return;
}
//delete existing in case page is refreshed or something
EEM_Line_Item::instance()->delete(
array(
array(
'TXN_ID' => $transaction->ID(),
'LIN_name' => $cart_modifier_name,
)
)
);
$amount_owing_before = $payment_options_reg_step->checkout->amount_owing;
// check payment type here instead to remove surcharge when not using square
if ( in_array( $selected_method_of_payment, $payment_methods_with_surcharges, true ) ) {
$surcharge_line_item_id = EEH_Line_Item::add_percentage_based_item(
$total_line_item,
$cart_modifier_name,
$cart_modifier_amount,
$cart_modifier_description,
$cart_modifier_taxable
);
}
// if ($surcharge_line_item_id) {
$total_line_item->recalculate_total_including_taxes();
$payment_options_reg_step->checkout->amount_owing += ($total_line_item->total() - $amount_owing_before);
$transaction->set_total($total_line_item->total());
$success = $transaction->save();
if ($success) {
/** @type EE_Registration_Processor $registration_processor */
$registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
$registration_processor->update_registration_final_prices($transaction);
}
// }
}
// the switch_payment_method hook makes more sense
add_action( 'AHEE__Single_Page_Checkout__before_payment_options__switch_payment_method', 'bc_add_cart_modifier', 10, 1 );
//add_action( 'AHEE__Single_Page_Checkout__before_payment_options__process_reg_step', 'bc_add_cart_modifier', 10, 1 );
// Reload the page once a payment method has been switched.
function tw_ee_refresh_checkout_on_payment_method_change() {
wp_add_inline_script(
'single_page_checkout',
'jQuery(function () {
SPCO.main_container.on( \'spco_switch_payment_methods\', function() {
window.location.reload();
});
});'
);
}
add_action( 'wp_enqueue_scripts', 'tw_ee_refresh_checkout_on_payment_method_change', 11 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment