当税前购物车总价值低于设定金额时设置零税 - woocommerce

问题描述 投票:0回答:2

我一直在阅读这篇文章 为 110 美元以下的小计设置“零税” - Woocommerce 作为在购物车总价值时分配“零”税的参考 < 800 (otherwise it applies another tax class). In my case I applied it only to parcels shipped to US. Upon testing the code, it seems that the $subtotal loop returns 0 instead of the real total cart value before taxes ($400). Any idea as to why? Below is my edited code. Thanks!

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;

     if ( WC()->customer->get_shipping_country() !== 'US' )
        return;

         $defined_amount = 800;
         $subtotal = 0;

        foreach ( $cart->get_cart() as $cart_item ) {
        $subtotal += $cart_item['line_total'];
        }

        if ( $subtotal < $defined_amount )
        return;

        foreach ( $cart->get_cart() as $cart_item ) {
        $cart_item['data']->set_tax_class( 'zero' );
        }
}
php woocommerce checkout subtotal tax
2个回答
0
投票

调试后,以下编辑的代码对我有用:

add_action( 'woocommerce_before_calculate_totals', 'apply_conditionally_zero_tax_rate', 10, 1 );

function apply_conditionally_zero_tax_rate( $cart ) {
     

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

    if ( WC()->customer->get_shipping_country() !== 'US' )
        return;
  

    $defined_amount = 800;
    $subtotal = 0;

  
    foreach ( $cart->get_cart() as $cart_item ) {
    $subtotal += $cart_item['line_total'];
    }
       

    if ( $subtotal > $defined_amount ){

      foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
      $cart_item['data']->set_tax_class( 'zero' );
      }
    }  
}

0
投票

我一直在使用上面的代码(感谢创建它的人)来做一些非常类似的事情。由于某种原因,当我将 iten 添加到购物车时,出现以下错误:

警告:/home/bullsey4/public_html/can/wp-content/plugins/code-snippets/php/snippet-ops.php(581) 中未定义的数组键“line_total”:第 29 行上的 eval() 代码

任何人都知道为什么会发生这种情况。顺便说一句,如果我添加额外的相同项目,我不会收到错误。

谢谢

© www.soinside.com 2019 - 2024. All rights reserved.