为小计设置$ 110以下的“零税"-Woocommerce [英] Set 'Zero Tax' for subtotal under $110 - Woocommerce

查看:57
本文介绍了为小计设置$ 110以下的“零税"-Woocommerce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为低于$ 110 的订单设置免税.

我碰到了这个线程根据Woocommerce中的购物车价格有条件地设置不同的税率,并尝试了它,但是当我将它添加到functions.php上时似乎不起作用-也许是因为某些代码已经过时了?

这是修改后的代码:

  add_action('woocommerce_before_calculate_totals','change_cart_items_prices',10,1);函数change_cart_items_prices($ cart){如果(is_admin()&&!defined('DOING_AJAX'))返回;如果(did_action('woocommerce_before_calculate_totals')> = 2)返回;foreach($ cart-> get_cart()as $ cart_item){//获得产品价格$ price = $ cart_item ['data']-> get_price();//根据价格有条件地设置税种如果($ price< 111)$ cart_item ['data']-> set_tax_class('zero-rate');//低于111如果($ price> = 111)$ cart_item ['data']-> set_tax_class('standard');//等于110以上}} 

我相信Standard&Woo Commerce>设置>税中的零税率税选项已正确设置.

解决方案

使用以下代码使其正常工作:

  add_action('woocommerce_before_calculate_totals','apply_conditionally_zero_tax_rate',10,1);函数apply_conditionally_zero_tax_rate($ cart){如果(is_admin()&&!defined('DOING_AJAX'))返回;如果(did_action('woocommerce_before_calculate_totals')> = 2)返回;$ defined_amount = 110;$小计= 0;//遍历购物车项目(第一个循环-获取购物车小计)foreach($ cart-> get_cart()as $ cart_item){$小计+ = $ cart_item ['line_total'];}//定位到总计不超过定义的数量"的购物车如果($小计> $ defined_amount)返回;//遍历购物车项目(第二遍-更改税率)foreach($ cart-> get_cart()as $ cart_item){$ cart_item ['data']-> set_tax_class('zero-rate');}} 

I'm trying to setup no tax for orders under $110.

I bumped into this thread Set different Tax rates conditionally based on cart item prices in Woocommerce and tried it but doesn't seems to work when i add it on functions.php - Maybe because some of the codes are outdated?

Here's the revised code:

add_action( 'woocommerce_before_calculate_totals', 'change_cart_items_prices', 10, 1 );
function change_cart_items_prices( $cart ) {

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

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

foreach ( $cart->get_cart() as $cart_item ) {
    // get product price
    $price = $cart_item['data']->get_price();

    // Set conditionaly based on price the tax class
    if ( $price < 111 )
        $cart_item['data']->set_tax_class( 'zero-rate' ); // below 111
    if ( $price >= 111 )
        $cart_item['data']->set_tax_class( 'standard' ); // Equal above 110
  }
}

I believe Standard & Zero rates tax options in Woo commerce > Settings > Tax are setup correctly.

解决方案

Got it working using this code:

add_action( 'woocommerce_before_calculate_totals', 'apply_conditionally_zero_tax_rate', 10, 1 );
function apply_conditionally_zero_tax_rate( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    $defined_amount = 110;
    $subtotal = 0;

    // Loop through cart items (1st loop - get cart subtotal)
    foreach ( $cart->get_cart() as $cart_item ) {
        $subtotal += $cart_item['line_total'];
    }

    // Targeting cart subtotal up to the "defined amount"
    if ( $subtotal > $defined_amount )
        return;

    // Loop through cart items (2nd loop - Change tax rate)
    foreach ( $cart->get_cart() as $cart_item ) {
        $cart_item['data']->set_tax_class( 'zero-rate' );
    }
}

这篇关于为小计设置$ 110以下的“零税"-Woocommerce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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