2017-06-06 47 views
0

我的购物车需要有免费送货,如果一个特定的产品与航运类是在那里,并且该产品的数量超过8。没关系,如果它购物车上有另一种产品。运费将免费。WooCommerce - 免费送货如果航运类是真实的,数量超过一定数量

因为我现在正在寻找航运类(这是正确的),但计数整个购物车的数量。不是在购物车上装运航空类的具体产品。我怎样才能在PHP中做到这一点?

回答

0
add_filter('woocommerce_package_rates', 'show_hide_free_shipping', 10, 2); 
function show_hide_free_shipping($available_shipping_methods, $package){ 
    $minimum_number_of_item = 12; //Give here minumum number of items to allow freeshipping 
    $shop_country = 'AU'; //Give here the country code where the store located 

    $free_shipping = false; 
    global $woocommerce; 
    $customer_country = $woocommerce->customer->get_shipping_country(); 

    $item_count = 0; 
    foreach (WC()->cart->cart_contents as $key => $item) { 
     $item_count += $item['quantity']; 
    } 

    if($item_count >= $minimum_number_of_item && $customer_country == $shop_country){ 
     $free_shipping = true; 
    } 

    if($free_shipping){ 
     foreach($available_shipping_methods as $shipping_method => $method){ 
      if(strpos($shipping_method, 'free_shipping') === false) { 
       unset($available_shipping_methods[$shipping_method]); 
      } 
     } 
    } 
    else{ 
     foreach($available_shipping_methods as $shipping_method => $method){ 
      if(strpos($shipping_method, 'free_shipping') !== false) { 
       unset($available_shipping_methods[$shipping_method]); 
      } 
     } 
    } 
    return $available_shipping_methods; 
} 

从下面的链接得到。 你可以使用它进行一些调整。

https://www.xadapter.com/showhide-free-shipping-based-item-count-domestic-shipment/

https://www.xadapter.com/hide-shipping-methods-shipping-classes-exist-not-exist-cart/

相关问题