duanpen9294 2018-09-21 12:39
浏览 90

WooCommerce免费送货给会员导致错误

I have this issue where I want for members to receive free delivery. I have figured out how to do that, but now I get an error on the page.

The error is:

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: 

This is the code that generates the problem:

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;
    }

I have tried with:

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

And still no luck.

  • 写回答

1条回答 默认 最新

  • dongqiao1964 2018-09-21 13:41
    关注

    The Taxes for shipping methods rates are set in a more complex multidimensional array, so your code make an error. Also you just forgot to null the rate cost.

    You may have to "Enable debug mode" in general shipping settings under "Shipping options" tab, to disable temporarily shipping caches.

    Try the following code that will null any shipping method cost for a specific country (DK) and for active members:

    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;
    }
    

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

    Don't forget to enable back shipping cache.

    评论

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”