Disable tax programmatically for a specific user role

拥有回忆 提交于 2020-01-04 06:22:06

问题


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


回答1:


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:

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

Third - Then this function hooked in woocommerce_product_tax_class will do the trick:

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', 1, 2 );

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

This code goes in function.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



来源:https://stackoverflow.com/questions/39972780/disable-tax-programmatically-for-a-specific-user-role

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!