2017-06-06 16 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/

相關問題