Advertisement
Guest User

Event Espresso multi-region tax support

a guest
Apr 1st, 2020
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.43 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Event Espresso Regional Tax Solution
  4. Description: Apply correct tax for event location in a way that reduces tax on promo code discounts
  5. */
  6.  
  7. /**
  8.  * bc_ee_apply_transaction_surcharge
  9.  *
  10.  * @param \EE_Checkout $checkout
  11.  * @return \EE_Checkout
  12.  */
  13. function bc_ee_apply_transaction_surcharge( EE_Checkout $checkout ) {
  14.     if ( $checkout instanceof EE_Checkout ) {
  15.         $transaction = $checkout->transaction;
  16.         if ( $transaction instanceof EE_Transaction ) {
  17.             $registrations = $transaction->registrations();
  18.             $registration = reset( $registrations );
  19.             if ( $registration instanceof EE_Registration ) {
  20.                 $event = $registration->event();
  21.                 $event_location = "";
  22.                 if ( $event instanceof EE_Event ) {
  23.                     $venue = $event->get_first_related('Venue');
  24.                     if($venue instanceof EE_Venue) {
  25.                         $event_location = $venue->state();
  26.                     }
  27.                     // Add cases for each of the regions (states/provinces)
  28.                     // Also add a function at the bottom of this for each additional region
  29.                     switch ( $event_location ) {
  30.                         case 'ON' :
  31.                             // apply the surcharge
  32.                             add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_true' );
  33.                             // hook into function below to set surcharge details
  34.                             add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__surcharge_details', 'ee_ontario_surcharge_details' );
  35.                             break;
  36.                         case 'AB' :
  37.                             // apply the surcharge
  38.                             add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', '__return_true' );
  39.                             // hook into function below to set surcharge details
  40.                             add_filter( 'FHEE__bc_ee_apply_transaction_surcharge__surcharge_details', 'ee_alberta_surcharge_details' );
  41.                             break;
  42.                     }
  43.                 }
  44.             }
  45.         }
  46.     }
  47.  
  48.     // create default instance only if the above didn't set the surcharge details
  49.     $surcharge_details = apply_filters(
  50.         'FHEE__bc_ee_apply_transaction_surcharge__surcharge_details',
  51.         array(
  52.             'name'        => 'Ontario HST',                 //  name for surcharge that will be displayed
  53.             'code'        => 'ontario-hst',                 // unique code used to identify surcharge in the db
  54.             'description' => 'Ontario HST 13%',             // description for line item
  55.             'percent'     => 13,                            // percentage amount
  56.             'taxable'     => false,                         // whether or not tax is applied on top of the surcharge
  57.         )
  58.     );
  59.  
  60.     // apply the surcharge ?
  61.     if ( ! apply_filters( 'FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge', false )) {
  62.         return $checkout;
  63.     }
  64.     // verify checkout
  65.     if ( ! $checkout instanceof EE_Checkout ) {
  66.         return $checkout;
  67.     }
  68.     // verify cart
  69.     $cart = $checkout->cart;
  70.     if ( ! $cart instanceof EE_Cart ) {
  71.         return $checkout;
  72.     }
  73.     // verify grand total line item
  74.     $grand_total = $cart->get_grand_total();
  75.     if ( ! $grand_total instanceof EE_Line_Item ) {
  76.         return $checkout;
  77.     }
  78.     // has surcharge already been applied ?
  79.     $existing_surcharge = $grand_total->get_child_line_item( $surcharge_details[ 'code' ] );
  80.     if ( $existing_surcharge instanceof EE_Line_Item ) {
  81.         return $checkout;
  82.     }
  83.     EE_Registry::instance()->load_helper( 'Line_Item' );
  84.     $pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal( $grand_total );
  85.     $pre_tax_subtotal->add_child_line_item(
  86.         EE_Line_Item::new_instance( array(
  87.             'LIN_name'       => $surcharge_details[ 'name' ],
  88.             'LIN_desc'       => $surcharge_details[ 'description' ],
  89.             'LIN_unit_price' => 0,
  90.             'LIN_percent'    => $surcharge_details[ 'percent' ],
  91.             'LIN_quantity'   => NULL,
  92.             'LIN_is_taxable' => $surcharge_details[ 'taxable' ],
  93.             'LIN_order'      => 0,
  94.             'LIN_total'      => (float) ( $percentage_amount * ( $pre_tax_subtotal->total() / 100 ) ),
  95.             'LIN_type'       => EEM_Line_Item::type_line_item,
  96.             'LIN_code'       => $surcharge_details[ 'code' ],
  97.         ) )
  98.     );
  99.     $grand_total->recalculate_total_including_taxes();
  100.     return $checkout;
  101. }
  102. add_filter( 'FHEE__EED_Single_Page_Checkout___initialize_checkout__checkout', 'bc_ee_apply_transaction_surcharge' );
  103.  
  104. /*
  105.     Add a new function for each region needed as well as adding a case
  106.     to the switch in the bc_ee_apply_transaction_surcharge function above
  107. */
  108. /**
  109.  * @return array
  110.  */
  111. function ee_ontario_surcharge_details() {
  112.     return array(
  113.         'name'          => 'Ontario HST',
  114.         'code'          => 'ontario-hst',
  115.         'description'   => 'Ontario HST 13%',
  116.         'percent'       => 13,
  117.         'taxable'       => false,
  118.     );
  119. }
  120. /**
  121.  * @return array
  122.  */
  123. function ee_alberta_surcharge_details() {
  124.     return array(
  125.         'name'          => 'Alberta GST',
  126.         'code'          => 'alberta-gst',
  127.         'description'   => 'Alberta GST 5%',
  128.         'percent'       => 5,
  129.         'taxable'       => false,
  130.     );
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement