以编程方式禁用特定用户角色的税收 [英] Disable tax programmatically for a specific user role
问题描述
在woocommerce网站上,我已在常规WooCommerce设置中启用了Tax.
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.
我怎么能做到这一点?
谢谢
推荐答案
2020更新
您不能以编程方式禁用特定用户角色的WooCommerce税,但是您可以以零税率申请特定用户角色.
首先,您需要在worpress中设置此特定用户角色.如果是这种情况,那么在我的代码示例中,此自定义用户角色为 'resellers'
.
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.
第二,您必须在WooCommerce设置中启用零税率:
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:
第三步-然后挂在 woocommerce_product_tax_class
上的此函数将完成此任务:
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', 10, 2 );
更新-由于WooCommerce 3改为使用以下内容:
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 );
您只需要放置所需的用户角色插件即可,而不是转售商".
You will just need to put instead of 'resellers' your desired user role slug.
此代码会出现在您活动的子主题(或主题)的functions.php文件中,也可能会出现在任何插件文件中.
此代码已经过测试,功能齐全.
This code is tested and fully functional.
参考: WooCommerce -启用零费率";某些特定用户角色的税收类别
这篇关于以编程方式禁用特定用户角色的税收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!