dpoxk64080 2016-10-03 16:25
浏览 47
已采纳

WooCommerce - 为某些特定用户角色启用“零利率”税级

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条回答 默认 最新

  • duandou9931 2016-10-03 19:15
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序