【问题标题】:Disable tax programmatically for a specific user role以编程方式为特定用户角色禁用税收
【发布时间】:2017-02-19 17:33:06
【问题描述】:

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

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

我怎样才能做到这一点?

谢谢

【问题讨论】:

    标签: php wordpress woocommerce user-roles tax


    【解决方案1】:

    2020 年更新

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

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

    其次,您必须在 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 );
    

    在 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_tax_class', 'zero_rate_for_custom_user_role', 10, 2 );
    

    您只需要放置您想要的用户角色而不是“经销商”。

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

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

    参考:WooCommerce - Enabling "Zero rate" tax class to some specific user roles

    【讨论】:

    • 如果我想为非登录用户禁用怎么办?
    • @LoicTheAztec “零利率”适用于简单产品,但不适用于变体产品。请帮忙
    • 这在 WooCommerce 版本 5.2.2 上对我不起作用。如果有人可以更新它并让它为 Woo 5+ 工作,那就太棒了
    • @MattWilson 抱歉,复合挂钩 woocommerce_product_get_tax_classwoocommerce_product_variation_get_tax_class在 WooCommerce 最新版本中仍然可以正常工作,但它需要输入不含税的产品价格,以便在更改税级时获得明显的差异以编程方式。
    猜你喜欢
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 2017-04-13
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    相关资源
    最近更新 更多