0

I have been reading this article Set 'Zero Tax' for subtotal under $110 - Woocommerce as reference to assign ‘zero’ taxes when total cart value < 800 (otherwise it applies another tax class). In my case I applied it only to parcels shipped to US. Upon testing the code, it seems that the $subtotal loop returns 0 instead of the real total cart value before taxes ($400). Any idea as to why? Below is my edited code. Thanks!

add_action( 'woocommerce_before_calculate_totals', 'apply_conditionally_zero_tax_rate', 10, 1 );

    function apply_conditionally_zero_tax_rate( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

     if ( WC()->customer->get_shipping_country() !== 'US' )
        return;

         $defined_amount = 800;
         $subtotal = 0;

        foreach ( $cart->get_cart() as $cart_item ) {
        $subtotal += $cart_item['line_total'];
        }

        if ( $subtotal < $defined_amount )
        return;

        foreach ( $cart->get_cart() as $cart_item ) {
        $cart_item['data']->set_tax_class( 'zero' );
        }
}
Manu
  • 21
  • 7
  • I don't immediately see an error in your code. Have you debugged to know the subtotal value effectively? are you perhaps using other custom code or plugin that could be of influence? – 7uc1f3r Sep 23 '21 at 11:15
  • @7uc1f3r - Thanks for checking my code. The way I tested it is by setting $defined_amount = 0 and $subtotal = 10. Then I set the following condition if ($subtotal = $defined_amount) into the code, which returned 'zero' tax class. So I assumed that the loop has overridden the initial $subtotal value of 10 with zero. When I set condition if ($subtotal > $defined_amount), the 'other' tax class is selected. – Manu Sep 23 '21 at 11:37
  • 2
    If you tested this by setting `$subtotal` to 10, then the 10 can never be overwritten by 0, even if the loop is executed. Because in the loop the line_total is added to the `$subtotal` so it can only be 10 or more than 10. As I mentioned, [debugging](https://stackoverflow.com/questions/61740111/how-to-debug-in-woocommerce-3) seems to be the only correct way to come to a solution – 7uc1f3r Sep 23 '21 at 12:59
  • This is very helpful indeed. Thank you for your suggestion. Manu – Manu Sep 23 '21 at 16:41

1 Answers1

0

After debugging, the below edited code works for me:

add_action( 'woocommerce_before_calculate_totals', 'apply_conditionally_zero_tax_rate', 10, 1 );

function apply_conditionally_zero_tax_rate( $cart ) {
     

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;
              

    if ( WC()->customer->get_shipping_country() !== 'US' )
        return;
  

    $defined_amount = 800;
    $subtotal = 0;

  
    foreach ( $cart->get_cart() as $cart_item ) {
    $subtotal += $cart_item['line_total'];
    }
       

    if ( $subtotal > $defined_amount ){

      foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
      $cart_item['data']->set_tax_class( 'zero' );
      }
    }  
}
Manu
  • 21
  • 7