WooCommerce-启用“零费率"某些特定用户角色的税种 [英] WooCommerce - Enabling "Zero rate" tax class to some specific user roles

查看:112
本文介绍了WooCommerce-启用“零费率"某些特定用户角色的税种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在wy WooCommerce网站上,我将出售给分销商经销商.问题是代理商免征TAXES,因此我需要使用自定义功能来为某些客户角色启用零税率(如果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?

谢谢

推荐答案

2020更新

尝试使用基于您的代码的自定义功能,我将首先获得当前的用户角色.然后,我在if语句中使用 in_array() php条件函数将您的2个目标角色与当前用户角色进行比较.通过这种方式,我可以启用或禁用此零费率"税收类别.

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.

这是代码:

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 );

更新-由于WooCommerce 3改为使用以下内容:

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_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 );

此代码包含在活动子主题(或主题)的functions.php文件或任何插件文件中.

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.

这篇关于WooCommerce-启用“零费率"某些特定用户角色的税种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆