1

This has been answered a while back but the filter is not working anymore. Not sure if it is deprecated or not. I am using both filters:

  • woocommerce_product_tax_class
  • woocommerce_product_get_tax_class

My function looks like:

function wc_diff_rate_for_user( $tax_class, $product ) {
    $tax_class = "Zero rate";
    return $tax_class;
}
add_filter( 'woocommerce_product_tax_class', 'wc_diff_rate_for_user', 1, 2 );

How can I set a tax class based on specific coupon in Woocommerce?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Tyler Nichol
  • 635
  • 2
  • 7
  • 28

1 Answers1

1

Since Woocommerce 3, the filter hook woocommerce_product_tax_class doesn't exist anymore, only new woocommerce_product_get_tax_class composite filter hook is available and works.

There is multiple ways to set a tax class based on an applied coupon code (In both examples below, we set "Zero rate" tax class when a defined coupon code is applied):

1) Using woocommerce_before_calculate_totals action hook, the best way:

add_action( 'woocommerce_before_calculate_totals', 'change_tax_class_based_on_specific_coupon', 25, 1 );
function change_tax_class_based_on_specific_coupon( $cart ) {
    // Define your coupon code below
    if ( ! $cart->has_discount('summer') )
        return;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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


    foreach( $cart->get_cart() as $cart_item ){
        // We set "Zero rate" tax class
        $cart_item['data']->set_tax_class("Zero rate");
    }
}

Code goes in function.php file of the active child theme (or active theme). Tested and works.


2) Using woocommerce_product_get_tax_class filter hook:

add_filter( 'woocommerce_product_get_tax_class', 'change_tax_class_based_on_specific_coupon', 30, 2 );
function change_tax_class_based_on_specific_coupon( $tax_class, $product ) {
    // Define your coupon code below
    if( WC()->cart->has_discount('summer') )
        $tax_class = "Zero rate";

    return $tax_class;
}

Code goes in function.php file of the active child theme (or active theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • The first one worked great. Tried the second one too but nothing changed. Ill stick with the first one. Thanks for this! – Tyler Nichol Sep 29 '18 at 16:46