1

我有这个问题,我希望会员可以免费送货。我已经想出了如何做到这一点,但现在页面上出现错误。

错误是:

The WC_Cart->taxes function is deprecated since version 3.2. Replace 
with getters (WC_Cart::get_cart_contents_taxes()) and setters 
(WC_Cart::set_cart_contents_taxes())., referer: 

这是产生问题的代码:

add_filter('woocommerce_package_rates','test_overwrite_fedex', 100, 2);
  function test_overwrite_fedex($rates,$package) 
    {
      $memberships = wc_memberships_get_user_active_memberships();
      if (WC()->customer->get_shipping_country() === 'DK' && !empty($memberships))
        {
          foreach ($rates as $rate) 
            {
              //Set the TAX
              $rate->taxes[1] = 0;
            }
        }
        return $rates;
    }

我尝试过:

$rate->set_shipping_total('0');
WC()->cart->set_shipping_total('0');
$rate = WC()->cart->get_shipping_total();

仍然没有运气。

4

1 回答 1

0

运输方式税率的税费设置在一个更复杂的多维数组中,因此您的代码会出错。此外,您只是忘记取消费率成本。

您可能必须在“运输选项”选项卡下的常规运输设置中“启用调试模式”,以暂时禁用运输缓存。

尝试以下代码,该代码将使特定国家 (DK) 和活跃会员的任何运输方式成本无效:

add_filter('woocommerce_package_rates', 'conditionally_remove_shipping_rates_cost', 25, 2);
function conditionally_remove_shipping_rates_cost( $rates, $package ){

    $memberships = wc_memberships_get_user_active_memberships();

    if ( WC()->customer->get_shipping_country() === 'DK' && !empty($memberships) ) {

        // Loop through the shipping taxes array
        foreach ( $rates as $rate_key => $rate ){
            $has_taxes = false;

            // Not for free shipping
            if( 'free_shippping' !== $rate->method_id ){
                // Taxes rate cost (if enabled)
                $taxes = [];

                // Null the shippin cost
                $rates[$rate_key]->cost = 0;

                // Loop through the shipping taxes array (as they can be many)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $rates[$rate_key]->taxes[$key] > 0 ){
                        // Null tax cost
                        $taxes[$key] = 0;
                        $has_taxes   = true;
                    }
                }
                if( $has_taxes )
                    $rates[$rate_key]->taxes = $taxes;
            }
        }
    }
    return $rates;
}

代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作(没有隶属函数)

不要忘记启用回运缓存。

于 2018-09-21T13:41:31.290 回答