WooCommerce - Enabling “Zero rate” tax class to some specific user roles

亡梦爱人 提交于 2020-01-15 06:07:20

问题


In wy WooCommerce web site, I'm going to be selling to distributors AND resellers. The problem is that resellers are exempt from TAXES and therefore I need with a custom function to enable Zero taxe rate for certain customer roles (it would be optimal if WooCommerce did it on its own, but it does not).

So my problem is that the code I have works perfect except that I don't know how to implement a change to calculate zero taxes if the customer is administrator OR reseller.

Here is the code That I am using:

function wc_diff_rate_for_user( $tax_class, $product ) {

    if ( is_user_logged_in() && current_user_can( 'administrator' ) ) {
        $tax_class = 'Zero Rate';
    }
    return $tax_class;
}

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

How can I modify this code to make it work, for that users roles?

Thanks


回答1:


Try this customized function based on your code where I get first the current user roles. Then I use in_array() php conditional function in an if statement to compare your 2 targeted roles with the current user roles. This way I enable or not this 'Zero rate' tax class.

Here is the code:

function wc_diff_rate_for_user( $tax_class, $product ) {
    // Getting the current user 
    $current_user = wp_get_current_user();
    $current_user_data = get_userdata($current_user->ID);

    if ( in_array( 'administrator', $current_user_data->roles ) || in_array( 'reseller', $current_user_data->roles ) )
        $tax_class = 'Zero Rate';

    return $tax_class;
}
add_filter( 'woocommerce_product_tax_class', 'wc_diff_rate_for_user', 1, 2 );

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.



来源:https://stackoverflow.com/questions/39836170/woocommerce-enabling-zero-rate-tax-class-to-some-specific-user-roles

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