-1

I'm want to apply zero tax when sum is less than 100, but this code is not fired (I don't see print to console or echo is source). I added this to the code snippets plugin. What am I doing wrong?

add_filter( 'woocommerce_product_tax_class', 'big_apple_get_tax_class', 1, 2 );

function big_apple_get_tax_class( $tax_class, $product ) {
        echo "<script>console.log('Debug Objects' );</script>";

    global $woocommerce;
    $cart_subtotal = $woocommerce->cart->get_cart_subtotal();
    if ( $cart_subtotal <= 100 )
        $tax_class = 'Zero Rate';

    return $tax_class;
}
  • 1
    That filter seems to be deprecated. I think you may want to try: woocommerce_product_get_tax_class instead – wharfdale Nov 22 at 14:35
  • I agree! The hook woocommerce_product_tax_class is deprecated since version 3.2.6. Use woocommerce_product_get_tax_class – Sam Nov 22 at 15:45
  • thank you both but it's still not working – user1932595 Nov 23 at 21:25
  • Hey there! This is a WooCommerce question not a WordPress question, 3rd party plugins are offtopic here, you should contact WooCommerce dev support, FB groups, communities, meetup groups, etc – Tom J Nowell 7 hours ago
0

Here is Link which may be useful to you. I have tested and it is working fine for me. you have to just take care of woocommerce settings https://prnt.sc/q6fse9. such as Prices entered with tax or not.

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;

    $defined_amount = 100;
    $subtotal = 0;

    // Loop through cart items (1st loop - get cart subtotal)
    foreach ( $cart->get_cart() as $cart_item ) {
        $subtotal += $cart_item['line_total'];
    }

    // Targeting cart subtotal up to the "defined amount"
    if ( $subtotal > $defined_amount )
        return;

    // Loop through cart items (2nd loop - Change tax rate)
    foreach ( $cart->get_cart() as $cart_item ) {
        $cart_item['data']->set_tax_class( 'zero-rate' );
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.