根据 Woocommerce 中的特定优惠券设置税种 [英] Setting a tax class based on specific coupon in Woocommerce

查看:37
本文介绍了根据 Woocommerce 中的特定优惠券设置税种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题已在不久前得到解答,但过滤器不再起作用.不确定它是否已弃用.我同时使用两个过滤器:

This has been answered a while back but the filter is not working anymore. Not sure if it is deprecated or not. I am using both filters:

  • woocommerce_product_tax_class
  • woocommerce_product_get_tax_class

我的函数看起来像:

function wc_diff_rate_for_user( $tax_class, $product ) {
    $tax_class = "Zero rate";
    return $tax_class;
}
add_filter( 'woocommerce_product_tax_class', 'wc_diff_rate_for_user', 1, 2 );

如何根据 Woocommerce 中的特定优惠券设置税种?

How can I set a tax class based on specific coupon in Woocommerce?

推荐答案

从 Woocommerce 3 开始,过滤器钩子 woocommerce_product_tax_class 不再存在,只有新的 woocommerce_product_get_tax_class 复合过滤器钩子可用并且有效.

Since Woocommerce 3, the filter hook woocommerce_product_tax_class doesn't exist anymore, only new woocommerce_product_get_tax_class composite filter hook is available and works.

有多种方法可以根据应用的优惠券代码设置税级 (在下面的两个示例中,我们在应用定义的优惠券代码时设置零税率"税级):

1) 使用 woocommerce_before_calculate_totals 动作钩子,最好的方法:

1) Using woocommerce_before_calculate_totals action hook, the best way:

add_action( 'woocommerce_before_calculate_totals', 'change_tax_class_based_on_specific_coupon', 25, 1 );
function change_tax_class_based_on_specific_coupon( $cart ) {
    // Define your coupon code below
    if ( ! $cart->has_discount('summer') )
        return;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;


    foreach( $cart->get_cart() as $cart_item ){
        // We set "Zero rate" tax class
        $cart_item['data']->set_tax_class("Zero rate");
    }
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

Code goes in function.php file of the active child theme (or active theme). Tested and works.

2) 使用 woocommerce_product_get_tax_class 过滤钩子:

2) Using woocommerce_product_get_tax_class filter hook:

add_filter( 'woocommerce_product_get_tax_class', 'change_tax_class_based_on_specific_coupon', 30, 2 );
function change_tax_class_based_on_specific_coupon( $tax_class, $product ) {
    // Define your coupon code below
    if( WC()->cart->has_discount('summer') )
        $tax_class = "Zero rate";

    return $tax_class;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

Code goes in function.php file of the active child theme (or active theme). Tested and works.

这篇关于根据 Woocommerce 中的特定优惠券设置税种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆