Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GST, HST, PST, and QST for Canada eh? Event Espresso 4.
<?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 ACTIVATE THE "ADDRESS QUESTION GROUP"
* THEN SET THE STATE/PROVINCE QUESTION TO BE REQUIRED
*
* BECAUSE THIS QUESTION SHOULD ONLY BE ASKED ONCE PER TRANSACTION
* THE ADDRESS QUESTION GROUP SHOULD NOT BE SET FOR ADDITIONAL REGISTRANTS
* IN OTHER WORDS,
* CHECK THE BOX NEXT TO ADDRESS IN THE "Questions for Primary Registrant"
* SIDEBAR METABOX ONLY
*
*
* @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 === 'state' ) {
switch ( $response ) {
case '60' :
case '70' :
case '71' :
case '72' :
// apply 5% surcharge Alberta, NWT, Nunavet, Yukon
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_true' );
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__surcharge_details', 'ee_al_north_surcharge_details' );
break;
case '61' :
case '62' :
// apply 12% surcharge BC, Manitoba
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_true' );
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__surcharge_details', 'ee_bc_mb_surcharge_details' );
break;
case '63' :
case '64' :
case '65' :
case '67' :
// apply 15% surcharge NB, NL, NS, PE
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_true' );
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__surcharge_details', 'ee_nb_nl_ns_pe_surcharge_details' );
break;
case '66' :
// apply 13% surcharge Ontario
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_true' );
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__surcharge_details', 'ee_on_surcharge_details' );
break;
case '68' :
// apply 14.975% surcharge Quebec
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_true' );
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__surcharge_details', 'ee_qc_surcharge_details' );
break;
case '69' :
// apply 11% surcharge SK
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_true' );
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__surcharge_details', 'ee_sk_surcharge_details' );
break;
default :
add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_false' );
}
}
}
}
}
}
}
add_action( 'AHEE__EE_System__core_loaded_and_ready', 'bc_ee_determine_whether_to_apply_surcharge', 1 );
function ee_al_north_surcharge_details() {
return array(
'name' => 'gst',
'code' => 'gst',
'description' => 'gst 5%',
'percent' => 5,
'taxable' => false,
);
}
function ee_bc_mb_surcharge_details() {
return array(
'name' => 'gst + pst',
'code' => 'gst-pst',
'description' => 'gst 5% + pst 7%',
'percent' => 12,
'taxable' => false,
);
}
function ee_nb_nl_ns_pe_surcharge_details() {
return array(
'name' => 'hst',
'code' => 'hst',
'description' => 'hst 15%',
'percent' => 15,
'taxable' => false,
);
}
function ee_on_surcharge_details() {
return array(
'name' => 'ontario hst',
'code' => 'ontario-hst',
'description' => 'ontario hst 13%',
'percent' => 13,
'taxable' => false,
);
}
function ee_qc_surcharge_details() {
return array(
'name' => 'quebec gst + qst',
'code' => 'quebec-gst-qst',
'description' => 'quebec gst + qst 14.975%',
'percent' => 14.975,
'taxable' => false,
);
}
function ee_sk_surcharge_details() {
return array(
'name' => 'sk gst + pst',
'code' => 'sk-gst-pst',
'description' => 'gst 5% + pst 6%',
'percent' => 11,
'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' => 'gst',
// unique code used to identify surcharge in the db
'code' => 'province-gst',
// description for line item
'description' => 'province gst',
// percentage amount
'percent' => 5,
// 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' );
@sbourk

This comment has been minimized.

Copy link

@sbourk sbourk commented Oct 29, 2020

exactly what I needed!

@joshfeck Since I've been using this code to charge the right taxes to the registrants, the total displayed on the invoice is not correct. It is charging the taxes twice (once with the surcharge, and a second time with actual taxes). Checkout page works, but the invoice is not adding up properly.

More information:
The surcharge is added to the subtotal, and then it adds the taxes (which were disabled on the ticket selector. Strange)
I tried to modify the code right into the invoice "message", from the admin UI, but it doesn't work well.
I looked for a snippet of code, but didn't find any.
I tried to find my way to the code of the invoice thinking I could build a function to override it, but didn't figure it out.

image

Any help would be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment