<?php | |
/** | |
* Hide shipping rates when free shipping is available. | |
* Updated to support WooCommerce 2.6 Shipping Zones. | |
* | |
* @param array $rates Array of rates found for the package. | |
* @return array | |
*/ | |
function my_hide_shipping_when_free_is_available( $rates ) { | |
$free = array(); | |
foreach ( $rates as $rate_id => $rate ) { | |
if ( 'free_shipping' === $rate->method_id ) { | |
$free[ $rate_id ] = $rate; | |
break; | |
} | |
} | |
return ! empty( $free ) ? $free : $rates; | |
} | |
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 ); |
This comment has been minimized.
This comment has been minimized.
I have the same questions, I want to keep the Local Pickup option if only free shipping is available. "Local Pickup" (Instance ID: 5) |
This comment has been minimized.
This comment has been minimized.
@jamiet @pasztig: I've expanded on the provided function to hide all shipping options, except a custom shipping option I've added through a plugin.
Ofcourse you should be able replace the part I've not tested the last 2 suggestions, but it should at least set you on the right track |
This comment has been minimized.
This comment has been minimized.
@Sjouw You are an absolute king! Really saved me a headache as I had to still show "Local Pickup" with free shipping. Also to confirm, I used the label instead |
This comment has been minimized.
This comment has been minimized.
Thank you Sjouw, yours is the 1st version I've found that allows me to hide only one specific shipping method in Woocommerce when free shipping is implemented instead of all other shipping methods. Thank you for sharing! |
This comment has been minimized.
This comment has been minimized.
@Sjouw I added the following code but it still doesn't show an option for Local Pickup, what am I doing wrong?
|
This comment has been minimized.
This comment has been minimized.
@EBonar try this add_filter('woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2); |
This comment has been minimized.
This comment has been minimized.
@masjo this dose not work. |
This comment has been minimized.
This comment has been minimized.
I use two shipping Zone with different Minimum amount to get free shipping. I noticed that it will work automaticle. I forgot to add a free shipping in the Germany Zone. |
This comment has been minimized.
This comment has been minimized.
Hi I have a situation below:
How can I do this. I tried setting up the Shipping Zone Using the function function my_hide_shipping_when_free_is_available( $rates ) {
} following things doesn't happen.
How can I achieve this. Please suggest. |
This comment has been minimized.
This comment has been minimized.
@masjo works very great!!!! thanks a thousand time. what i now need that the free shipping the first option is selected by default (at the moment none of the shipping mehthods are seleced. |
This comment has been minimized.
This comment has been minimized.
Don't work for me :/ |
This comment has been minimized.
This comment has been minimized.
How would I hide other non-relevant shipping methods please? So if shipping for 1-3 items is $7 and 4-8 is $10, how would I hide the shipping option for $7, if the buyer has chosen 4-8 products please? |
This comment has been minimized.
This comment has been minimized.
Hey, so if you want to hide everything but free shipping and local pickup then modify @mikejolley code and change the foreach loop as follows:
|
This comment has been minimized.
This comment has been minimized.
Hello I want to display free shipping only when cart price is above 100 euros or if current user has a specific active membership and hide others shipping methods if possible
If you could help me that would be really nice of you Thanks Cheers |
This comment has been minimized.
This comment has been minimized.
I added a field to categories to store a date. Using this same code, I exclude Flat_Rate option when the date in the custom field is greater than the current date. |
This comment has been minimized.
This comment has been minimized.
i used this code add_filter( 'woocommerce_package_rates', 'businessbloomer_hide_free_shipping_for_shipping_class', 10, 2 ); function businessbloomer_hide_free_shipping_for_shipping_class( $rates, $package ) { But after i need remove Clear customer sessions after that, then it work., |
This comment has been minimized.
This comment has been minimized.
May I ask why it's not considered a given that other shipping methods would be hidden if free is available? What would the case be where you would want other shipping methods to choose from? I can only think it may to do with expedited delivery options. Is that correct? |
This comment has been minimized.
This comment has been minimized.
Hello can any one help me on the below issue i want to hide flat rate when order eligible for fee shipping and only free shipping and Local pick up should show Thanks |
This comment has been minimized.
This comment has been minimized.
Hi i am having problem with this. where exactly i can put this code. I did insert on function.php but its not working and got error on my admin panel. All i want is the shipping works by their geographical location. Base on the customer location and under my shipping zone it will only shows the shipping options with free shipping or flat rate |
This comment has been minimized.
This comment has been minimized.
Kindly help me out how can I change Zone regions postal code to cities ? I just want that when customer purchase from my website they can directly place city and get shipping charges, No Postal code, no region. |
This comment has been minimized.
This comment has been minimized.
Hello, good afternoon, I need to know how the exact code would be to incorporate, since I have two free shipping limits that I need at the time to display one or the other, depending on the minimum amount of purchase. Thank you so much |
This comment has been minimized.
This comment has been minimized.
Hello, I would like to use the original code to display free shipping when I don't need to show flat rate anymore : it doesn't work. What could be wrong ? I've tried to write my own filter before finding this one, and I couldn't understand what could be wrong. Still my site is running normally offline and online, with all the other functions and plugins. |
This comment has been minimized.
Is it possible to remove one of the shipping options and not all of them if free shipping is available?