2017-02-17 6 views
5

WooCommerceでは、カートに含まれる重量のある商品を除いて、一定の金額以上の送料が無料である必要があります。重量と最低カート数に応じて送料無料

私は何をすべきか知っていますか?

ありがとうございました。

+0

カートの一部の商品は送料無料となりますが、他の商品は送料無料ではありませんか? –

+1

[mcve] –

+0

ちょっとしたことで質問を編集してください。250以上の軽量製品の場合のみ無料配送が必要です。重い商品には250以上の送料が必要です。 – spy

答えて

3

このカスタムコードは、送料無料方法を続けるとカートの量は、に任されたときや製品は重い(ここで未満20キロ)でない場合は、他の配送方法を非表示になります...へ250未満のご注文の場合は送料が無料になりますので、これをワゴコミのに設定してください(最後を参照)。

まず、あなたは体重がそれぞれのバリエーションでは、単純なまたは変数の製品の各重積(()内に設定されていることを確認する必要があります。ここでは、カートの小計が除く税は(であり、あなたは含めてそれを変更することができます。簡単税)

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 modificatio –

関連する問題