2017-06-26 82 views
1

我需要在購物車重量超過150磅後,移除購物車上的UPS運輸方式和結賬頁面。這是沿着我在想什麼...以編程方式刪除特定的送貨方式 - WooCommerce

function is_it_over_onefifty($available_methods){ 

     global $woocommerce; 
     if ($woocommerce->cart->cart_contents_weight > 150){ 
      //diable shipping 
      unset($available_methods['ups']); 
     } 
     else{ 

      //do nothing 

     } 
     return $available_methods; 
} 
add_filter('woocommerce_available_shipping_methods', 'is_it_over_onefifty', 10, 1); 
+0

你是Google嗎?這是第一個結果:http://www.bolderelements.net/support/knowledgebase/hide-shipping-method-based-on-weight/ –

+0

啊,哇,不能相信我錯過了。你會碰巧知道如何獲得運費ID?我目前正在尋找一種方法。 @cale_b –

回答

0

我已經開始評論,但它變得太大了。

下面是一些堅實的示例代碼這將讓你去你需要的地方。注意這個想法是從這裏取得的:http://www.bolderelements.net/support/knowledgebase/hide-shipping-method-based-on-weight/

add_filter('woocommerce_package_rates', 'hide_shipping_weight_based', 10, 2); 

function hide_shipping_weight_based($rates, $package) { 
    // if you don't know what the rates are, you can uncomment below to see all the rates that are available 
    // var_dump($rates); 

    // Set weight variable 
    $cart_weight = 0; 

    // Shipping rate to be excluded 
    $shipping_type = 'ups_'; 

    // Calculate cart's total 
    foreach(WC()->cart->cart_contents as $key => $value) { 
     $cart_weight += $value['data']->weight * $value['quantity']; 
    } 

    // only if the weight is over 150lbs find/remove specific carrier 
    if($cart_weight > 150) { 
     // loop through all of the available rates 
     foreach($rates AS $id => $data)) { 
      // if the rate id starts with "ups_", remove it 
      if (0 === stripos($id, $shipping_type) { 
       unset($rates[ $id ]); 
      } 
     } 
    } 

    return $rates; 
} 
+0

嘿感謝您的幫助,我得到一個'解析錯誤:語法錯誤,意外'未設置'(T_UNSET)' –

+0

所以 - 我有一個錯字,但根據我的答案 - 這是示例代碼 - 並期望您有一些排除故障/診斷的能力。如果沒有,您可能會沮喪嘗試破解WooCommerce ... –

+0

僅供參考 - 我修正了上面的錯字。 –