【问题标题】:WooCommerce free shipping to members results in errorWooCommerce 向会员免费送货导致错误
【发布时间】:2019-02-25 21:07:54
【问题描述】:

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

错误是:

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();

仍然没有运气。

【问题讨论】:

    标签: php wordpress woocommerce membership shipping-method


    【解决方案1】:

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

    您可能必须在“配送选项”标签下的常规配送设置中“启用调试模式”,才能暂时禁用配送缓存。

    尝试以下代码,该代码将取消特定国家 (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 文件中。经过测试并且可以工作(没有隶属函数)

    不要忘记启用回运缓存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-14
      • 2014-04-14
      • 2020-12-01
      • 2020-11-26
      • 2020-11-05
      • 2016-11-11
      相关资源
      最近更新 更多