2016-07-09 9 views
1

Woocommerceで無料配送が利用可能な場合、他の配送オプションを非表示にしたいと考えています。WooCommerce - 送料無料の他の配送方法を隠す

woocommerceの最新バージョンには、無料の配送オプションがある場合でも、他の配送オプションが表示されるようになりました。

WooCommerce 2.6+のための今回のコードスニペットは

答えて

10

あります助けてください。あなたが試すことができていること:、保存し、有効化、無効woocommerce出荷設定では、現在出荷ゾーンに関連した配送方法を保存します。

add_filter('woocommerce_package_rates', 'hide_other_shipping_when_free_is_available', 100, 2); 

function hide_other_shipping_when_free_is_available($rates, $package) { 

    $free = array(); 
    foreach ($rates as $rate_id => $rate) { 
     if ('free_shipping' === $rate->method_id) { 
      $free[ $rate_id ] = $rate; 
      break; 
     } 
    } 
    return ! empty($free) ? $free : $rates; 
} 

あなたは出荷キャッシュされたデータをリフレッシュする必要があります。


WooCommerce 2.5のために、あなたはこれを試してみてください:

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

function hide_shipping_when_free_is_available($rates, $package) { 

    // Only modify rates if free_shipping is present 
    if (isset($rates['free_shipping'])) { 

     // To unset a single rate/method, do the following. This example unsets flat_rate shipping 
     unset($rates['flat_rate']); 

     // To unset all methods except for free_shipping, do the following 
     $free_shipping   = $rates['free_shipping']; 
     $rates     = array(); 
     $rates['free_shipping'] = $free_shipping; 
    } 

    return $rates; 
} 

は、あなたのアクティブな子テーマやテーマにあるfunction.phpファイルにこのコードを貼り付けます。


参考:私はこの関数を呼び出すもの

+0

? –

+0

私はこのadd_filter( 'woocommerce_package_rates'、 'my_hide_shipping_when_free_is_available'、100)のようにこの関数を呼び出します。 function.phpで? @LoicTheAztec –

+0

あなたはそれを呼び出す必要はありません...アクティブなテーマのfunction.phpファイルにすべてのスニペットを貼り付けてください...これは仕事そのものを行います。どうして?この関数は、woocommerce coreの 'woocommerce_package_rates'にフックされているので、** [フック:アクションとフィルタのリファレンス](https://docs.woothemes.com/document/hooks/)** – LoicTheAztec

関連する問題