-
-
Save joshfeck/1b373714910eda57a46e397df61d97bf to your computer and use it in GitHub Desktop.
Adds surcharge line items for those that choose online payments.
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 | |
/* | |
Plugin Name: Event Espresso add custom online payments surcharges | |
Plugin URI: https://eventespresso.com/topic/is-there-a-way-to-add-surcharge-only-if-question-checkbox-is-checked/ | |
Description: If answer A then fee remains the same. If answer B then additional charges apply. | |
Version: 1.0 | |
Author: Event Espresso | |
Author URI: http://www.eventespresso.com | |
*/ | |
function bc_ee_determine_whether_to_apply_surcharge() { | |
$surcharge_QST_ID = 91; | |
if ( isset( $_REQUEST[ 'ee_reg_qstn' ] ) ) { | |
foreach ( $_REQUEST[ 'ee_reg_qstn' ] as $registrations ) { | |
if ( ! empty( $registrations ) ) { | |
foreach ( $registrations as $QST_ID => $response ) { | |
if ( $QST_ID === $surcharge_QST_ID ) { | |
switch ( $response ) { | |
case 'E-Check' : | |
// don't apply the surcharge | |
break; | |
case 'Credit/Debit Card' : | |
// apply the surcharge | |
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_true' ); | |
// hook into function below to set surcharge details | |
add_filter( 'FHEE__EED_Single_Page_Checkout___initialize_checkout__checkout', 'bc_ee_apply_transaction_surcharge', 10 ); | |
break; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
add_action( 'AHEE__EE_System__core_loaded_and_ready', 'bc_ee_determine_whether_to_apply_surcharge', 1 ); | |
function bc_ee_apply_transaction_surcharge( EE_Checkout $checkout ) { | |
$surcharge_details = array( | |
'name' => 'Transaction Fee', | |
'code' => 'percent-surcharge-fee', | |
'description' => 'convenience fee for online payments', | |
'percent' => 3.4, | |
'unit_price' => .25, | |
'taxable' => false, | |
); | |
// apply the surcharge ? | |
if ( ! apply_filters( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', false ) ) { | |
return $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; | |
} | |
// 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' => 0, | |
'LIN_percent' => $surcharge_details[ 'percent' ], | |
'LIN_quantity' => NULL, | |
'LIN_is_taxable' => $surcharge_details[ 'taxable' ], | |
'LIN_order' => 0, | |
'LIN_total' => (float) ( $percentage_amount * ( $pre_tax_subtotal->total() / 100 ) ), | |
'LIN_type' => EEM_Line_Item::type_line_item, | |
'LIN_code' => $surcharge_details[ 'code' ], | |
) ) | |
); | |
$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' => 'flat-fee', | |
) ) | |
); | |
$grand_total->recalculate_total_including_taxes(); | |
return $checkout; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment