2017-02-17 30 views
5

隨着WooCommerce,我需要有一定數量的免費送貨除了包含在購物車中的重型產品。免費送貨取決於重量和最小購物車的數量

有誰知道我該怎麼辦?

謝謝。

+0

所以你想購物車中的一些項目免費送貨,但不是爲了別人? –

+1

請按照[mcve] –

+0

編輯您的問題嘿,有點兒。我只需要免費送貨超過250輕產品。我需要擁有超過250個運輸成本的重型產品。 – spy

回答

3

這個自定義代碼將保持免費送貨方法,並隱藏其他的運輸方式時,車量最多爲,如果產品不重(小於超過20公斤這裏)...要不允許訂單少於250免費送貨,您可以在woocommerce (請參見最後)中進行設置。

首先,你必須確保重量在每個重產品設置的(簡單或變量產品(每個變化)。推車大部這裏是不含稅(你可以把它改成包括。易稅)

enter image description here

那麼這裏就是在woocommerce_package_rates過濾鉤子鉤住定製功能:

add_filter('woocommerce_package_rates', 'conditionally_hide_other_shipping_based_on_items_weight', 100, 1); 
function conditionally_hide_other_shipping_based_on_items_weight($rates) { 

    // Set HERE your targeted weight (here is 20 kg) <== <== <== <== <== 
    $target_product_weight = 20; 

    // Set HERE your targeted cart amount (here is 250) <== <== <== <== <== 
    $target_cart_amount = 250; 
    // For cart subtotal amount EXCLUDING TAXES 
    WC()->cart->subtotal_ex_tax >= $target_cart_amount ? $passed = true : $passed = false ; 
    // For cart subtotal amount INCLUDING TAXES (replace by this): 
    // WC()->cart->subtotal >= $target_cart_amount ? $passed = true : $passed = false ; 

    // Iterating trough cart items to get the weight for each item 
    foreach(WC()->cart->get_cart() as $cart_item){ 
     if($cart_item['variation_id'] > 0) 
      $item_id = $cart_item['variation_id']; 
     else 
      $item_id = $cart_item['product_id']; 

     // Getting the product weight 
     $product_weight = get_post_meta($item_id , '_weight', true); 

     if(!empty($product_weight) && $product_weight >= $target_cart_amount){ 
      $light_products_only = false; 
      break; 
     } 
     else $light_products_only = true; 
    } 

    // If 'free_shipping' method is available and if products are not heavy 
    // and cart amout up to the target limit, we hide other methods 
    $free = array(); 
    foreach ($rates as $rate_id => $rate) { 
     if ('free_shipping' === $rate->method_id && $passed && $light_products_only) { 
      $free[ $rate_id ] = $rate; 
      break; 
     } 
    } 

    return ! empty($free) ? $free : $rates; 
} 

代碼會出現在您的活動子主題(或主題)的function.php文件中,或者也存在於任何插件文件中。

此代碼測試,它適用於簡單和變型產品...

你也將在WooCommerce設置>航運,每個出貨區,爲"Free Shipping"方法的最低訂貨量:

enter image description here

您需要刷新運輸緩存數據:在woocommerce運輸設置中禁用,保存並啓用,保存當前運輸區域的相關運輸方式。

+0

非常感謝您的時間!!請問這隻會給重物提供每公斤運輸費用超過250而包含在購物車中的「輕」物品將免費送貨? – spy

+0

我不明白我爲此感到難過......所以輕便車上的重物會發生什麼情況?運費如我所願? – spy

0

對於超過一定數量的免費送貨,您可以使用內置的WooCommerce選項。

對於某些特定產品,如果跳過免費送貨,可以使用以下代碼段。

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

     function hide_shipping_method_if_particular_product_available_in_cart($available_shipping_methods) 
     { 
      global $woocommerce; 

      // products_array should be filled with all the products ids 
      // for which shipping method (stamps) to be restricted. 

      $products_array = array(
       101, 
       102, 
       103, 
       104 
      ); 

      // You can find the shipping service codes by doing inspect element using 
      // developer tools of chrome. Code for each shipping service can be obtained by 
      // checking 'value' of shipping option. 

      $shipping_services_to_hide = array(
       'free_shipping', 

      ); 

      // Get all products from the cart. 

      $products = $woocommerce->cart->get_cart(); 

      // Crawl through each items in the cart. 

      foreach($products as $key => $item) { 

       // If any product id from the array is present in the cart, 
       // unset all shipping method services part of shipping_services_to_hide array. 

       if (in_array($item['product_id'], $products_array)) { 
        foreach($shipping_services_to_hide as & $value) { 
         unset($available_shipping_methods[$value]); 
        } 

        break; 
       } 
      } 

      // return updated available_shipping_methods; 
    return 
     } 
+0

嘿那裏,謝謝你的片段。我只需要免費送貨超過250的輕產品。我需要擁有超過250個運輸成本的重型產品。是包括那個片段嗎?非常感謝 – spy

+0

免費送貨一定的最低金額可以通過Woocommerce設置。 –

+0

重型產品ID可以添加到片段,並可以管理與slght改性中 –

相關問題