以编程方式为特定用户角色禁用税收

2021-12-22 00:00:00 php wordpress woocommerce tax user-roles

在我的 woocommerce 网站中,我在 WooCommerce 的一般设置中启用了税.

我想以编程方式(使用任何钩子)从我的商店、结帐页面和订单电子邮件中禁用特定用户角色的税收.

我怎样才能做到这一点?

谢谢

解决方案

2020 更新

<块引用>

您不能以编程方式为特定用户角色禁用 WooCommerce 税,但您可以为特定用户角色申请零税率.

首先您需要在 worpress 中设置这个特定的用户角色.如果是这种情况,假设此自定义用户角色是我的代码示例的 'resellers'.

其次,您必须在 WooCommerce 设置中启用零税率:

然后对于每个国家/地区,您必须设置此零税率:

第三 - 然后这个钩子函数会起作用:

更新 - 从 WooCommerce 3 开始使用以下内容:

函数 zero_rate_for_custom_user_role( $tax_class, $product ) {//获取当前用户$current_user = wp_get_current_user();$current_user_data = get_userdata($current_user->ID);//<== <== <== <== <== <== <== 在这里放置您的用户角色 slugif ( in_array( 'resellers', $current_user_data->roles ) )$tax_class = '零税率';返回 $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);

在 WooCommerce 版本 3 之前使用以下内容:

函数 zero_rate_for_custom_user_role( $tax_class, $product ) {//获取当前用户$current_user = wp_get_current_user();$current_user_data = get_userdata($current_user->ID);//<== <== <== <== <== <== <== 在这里放置您的用户角色 slugif ( in_array( 'resellers', $current_user_data->roles ) )$tax_class = '零税率';返回 $tax_class;}add_filter('woocommerce_product_tax_class', 'zero_rate_for_custom_user_role', 10, 2 );

<块引用>

您只需要输入您想要的用户角色而不是经销商".

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

此代码经过测试且功能齐全.

参考:WooCommerce- 启用零利率";某些特定用户角色的税级

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

解决方案

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:

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

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

相关文章