4

In my woocommerce web site, I have enable Tax in general WooCommerce settings.

I would like to disable tax for a specific user role programmatically ( with any hooks ), from my shop, checkout page and from order email.

How could I achieve this?

Thanks

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Simanto
  • 51
  • 1
  • 5

1 Answers1

8

2020 update

You can't disable WooCommerce tax for a specific user role programmatically, but you can apply for a specific user role a zero tax rate.

First you need to have this specific user role set in worpress. If it's the case, let say that this custom user role is 'resellers' for my code example.

Second, you have to enable in WooCommerce settings a zero tax rate:

enter image description here

And then for each country, you will have to set this zero tax rate:

enter image description here

Third - Then this hooked function will do the trick:

Update - Since WooCommerce 3 use the following:

function zero_rate_for_custom_user_role( $tax_class, $product ) {
    // Getting the current user 
    $current_user = wp_get_current_user();
    $current_user_data = get_userdata($current_user->ID);
    
    //  <== <== <== <== <== <== <== Here you put your user role slug 
    if ( in_array( 'resellers', $current_user_data->roles ) )
        $tax_class = 'Zero Rate';

    return $tax_class;
}
add_filter( 'woocommerce_product_get_tax_class', 'wc_diff_rate_for_user', 10, 2 );
add_filter( 'woocommerce_product_variation_get_tax_class', 'wc_diff_rate_for_user', 10, 2 );

Before WooCommerce version 3 use the following:

function zero_rate_for_custom_user_role( $tax_class, $product ) {
    // Getting the current user 
    $current_user = wp_get_current_user();
    $current_user_data = get_userdata($current_user->ID);
    
    //  <== <== <== <== <== <== <== Here you put your user role slug 
    if ( in_array( 'resellers', $current_user_data->roles ) )
        $tax_class = 'Zero Rate';

    return $tax_class;
}
add_filter( 'woocommerce_product_tax_class', 'zero_rate_for_custom_user_role', 10, 2 );

You will just need to put instead of 'resellers' your desired user role slug.

This code goes in functions.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and fully functional.

Reference: WooCommerce - Enabling "Zero rate" tax class to some specific user roles

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • What if i want to disable for non logged in user? – Bilal Hussain Mar 15 '17 at 06:40
  • @LoicTheAztec the "Zero rate" is working fine for simple product but not working for Variation product. Please, help – Simanto May 18 '17 at 11:42
  • This did not work for me on WooCommerce Version 5.2.2. It would be amazing if someone could update this and get it working for Woo 5+ – Matt Wilson May 06 '21 at 19:53
  • @MattWilson Sorry but the composite hooks `woocommerce_product_get_tax_class` and `woocommerce_product_variation_get_tax_class`still work perfectly in WooCommerce last version, but it requires to enter product prices without taxes in order to get a visible difference when changing tax class programmatically. – LoicTheAztec May 06 '21 at 20:22
  • 1
    There is error in example, function names are different in hooks "wc_diff_rate_for_user" when they should be "zero_rate_for_custom_user_role" – Marko Jul 01 '22 at 10:40