-
-
Save Pebblo/957da75d1854fa84daf9362750bd8645 to your computer and use it in GitHub Desktop.
VAT 20 or 0? Event Espresso 4.
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 | |
/** | |
* PLEASE READ AND FOLLOW ALL INSTRUCTIONS IN CAPS | |
* | |
* IN ORDER FOR THIS TO WORK YOU NEED TO ADD A QUESTION USED FOR A TAX NUMBER | |
* | |
* BECAUSE THIS QUESTION SHOULD ONLY BE ASKED ONCE PER TRANSACTION | |
* YOU SHOULD ADD THE ABOVE QUESTION TO A GROUP ONLY ASKED FOR THE PRIMARY REGISTRANT | |
* IN OTHER WORDS, CHECK THE BOX NEXT TO WHICHEVER GROUP YOU SET THIS QUESTION TO BE IN | |
* WITHIN THE "Questions for Primary Registrant" SIDEBAR METABOX ONLY | |
* | |
* CHANGE $QST_ID === 22 TO BE THE ID OF THE QUESTIO YOU CREATED ABOVE | |
* IF ANY VALUE IS SET IN THAT FIELD, 0% VAT IS CHARGED, OTHERWISE 20% | |
* | |
* @return void | |
*/ | |
function bc_ee_determine_whether_to_apply_surcharge() { | |
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 === 22 ) { | |
if( !empty($response) ) { | |
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_true' ); | |
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__surcharge_details', 'ee_tw_zero_vat' ); | |
} else { | |
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_true' ); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
add_action( 'AHEE__EE_System__core_loaded_and_ready', 'bc_ee_determine_whether_to_apply_surcharge', 1 ); | |
function ee_tw_zero_vat() { | |
return array( | |
'name' => 'VAT0', | |
'code' => 'VAT0', | |
'description' => 'VAT 0%', | |
'percent' => 0, | |
'taxable' => false, | |
); | |
} | |
/** | |
* 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 = apply_filters( | |
'FHEE__bc_ee_apply_transaction_surcharge__surcharge_details', | |
array( | |
// name for surcharge that will be displayed | |
'name' => 'VAT', | |
// unique code used to identify surcharge in the db | |
'code' => 'standard-vat', | |
// description for line item | |
'description' => 'Standard VAT', | |
// percentage amount | |
'percent' => 20, | |
// whether or not tax is applied on top of the surcharge | |
'taxable' => false, | |
) | |
); | |
// STOP EDITING | |
// 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) ( $surcharge_details[ 'percent' ] * ( $pre_tax_subtotal->total() / 100 ) ), | |
'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