2017-02-17 32 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改性中 –