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' );
}
}