Php 以编程方式禁用特定用户角色的税务

Php 以编程方式禁用特定用户角色的税务,php,wordpress,woocommerce,user-roles,tax,Php,Wordpress,Woocommerce,User Roles,Tax,在我的woocommerce网站中,我已在一般woocommerce设置中启用了税务 我想从我的商店、结帐页面和订单电子邮件以编程方式(使用任何挂钩)禁用特定用户角色的税务 我怎样才能做到这一点 谢谢2020更新 您不能以编程方式禁用特定用户角色的WooCommerce tax,但您可以为特定用户角色申请零税率。 首先,您需要在worpress中设置此特定用户角色。如果是这样的话,假设这个自定义用户角色对于我的代码示例是“分销商” 其次,您必须在WooCommerce设置中启用零税率: 然后,

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

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

我怎样才能做到这一点


谢谢

2020更新

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

首先,您需要在worpress中设置此特定用户角色。如果是这样的话,假设这个自定义用户角色对于我的代码示例是“分销商”

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

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

第三个-然后这个钩住的函数将实现以下功能:

更新-由于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 );
自定义用户角色($tax\u class,$product)的函数零费率{
//获取当前用户
$current_user=wp_get_current_user();
$current\u user\u data=get\u userdata($current\u user->ID);

//如果我想为未登录的用户禁用该怎么办?@LoicTheAztec“零速率”对于简单的产品可以正常工作,但对于变体产品不起作用。请,帮助这在WooCommerce版本5.2.2上对我不起作用。如果有人能更新它并让它为Woo 5+@MattWilson工作,那将是令人惊讶的。抱歉,复合挂钩
WooCommerce\u产品\u获得税类
WooCommerce\u产品\u变体_get_tax_class
在WooCommerce的最新版本中仍然可以很好地工作,但它需要输入不含税的产品价格,以便在以编程方式更改税类时获得明显的差异。
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 );