【问题标题】:WooCommerce - Enabling "Zero rate" tax class to some specific user rolesWooCommerce - 为某些特定用户角色启用“零税率”税类
【发布时间】:2017-02-11 16:01:15
【问题描述】:

在 wy WooCommerce 网站上,我将向分销商和经销商销售产品。问题是 经销商 是免税的,因此我需要一个自定义功能来为某些客户角色启用零税率(如果 WooCommerce 自己做这将是最佳的,但它没有)。

所以我的问题是我的代码完美无缺,只是如果客户是管理员或经销商,我不知道如何实施更改以计算零税。

这是我正在使用的代码:

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

如何修改此代码以使其适用于该用户角色?

谢谢

【问题讨论】:

  • 一个简单的 if 语句应该适合你。如果 $tax_class== '零税率' 则不计算额外税。否则,请计算额外税款。也许分享更多你的代码?

标签: php wordpress woocommerce user-roles tax


【解决方案1】:

2020 年更新

根据您的代码尝试此自定义功能,我首先获得当前用户角色。然后我在 if 语句中使用 in_array() php 条件函数将您的 2 个目标角色与当前用户角色进行比较。这样我就可以启用或不启用这个“零税率”税级。

代码如下:

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 文件中或任何插件文件中。

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

【讨论】:

  • 一如既往,你是个天才……没有你我该怎么办……XD
  • 非常有帮助,wc 大师。
猜你喜欢
  • 2017-10-18
  • 2017-04-13
  • 2021-08-12
  • 2020-11-17
  • 2018-03-22
  • 2017-08-12
  • 2017-06-30
  • 2021-01-01
  • 2015-06-21
相关资源
最近更新 更多