
You may have setup different shipping rates in your WooCommerce store. What you want and need to charge for shipping is different in each webshop and it is very hard to set things up exactly the way you need it. In this post I will show you how you can only show the cheapest shipping method or the most expensive one.
Only show the cheapest shipping rate
One of the reasons you might want to only show the cheapest rate is because you’re using the different shipping rates form different shipping carriers. To make it easier to your customers you may only want to to show the cheapest shipping rate. To filter out the other rates you can use the following custom code;
<?php | |
/** | |
* Only display the cheapest shipping rate | |
*/ | |
function my_only_show_cheapest_shipping_rate( $rates, $package ) { | |
$cheapest_method = ''; | |
// Loop through shipping rates | |
if ( is_array( $rates ) ) : | |
foreach ( $rates as $key => $rate ) : | |
// Set variables when the rate is cheaper than the one saved | |
if ( empty( $cheapest_method ) || $rate->cost < $cheapest_method->cost ) : | |
$cheapest_method = $rate; | |
endif; | |
endforeach; | |
endif; | |
// Return the cheapest rate when possible | |
if ( ! empty( $cheapest_method ) ) : | |
return array( $cheapest_method->id => $cheapest_method ); | |
endif; | |
return $rates; | |
} | |
add_action( 'woocommerce_package_rates', 'my_only_show_cheapest_shipping_rate', 10, 2 ); |
This code will ensure only the cheapest shipping rate of ALL your rates is being shown. Keep in mind that this could also be free shipping if that is something you’re offering.
Only show the most expensive shipping rate
There are also different reasons why you may want to only show the most expensive shipping rate available. For example, if you’ve setup shipping rates with a cost based on the class. It could be that you can include some of the lighter items you sell within a heavier/bigger package. In that case it would be good to only show the most expensive shipping rate so you will not lose money on the shipping cost.
The following code is almost the same as the code above and can be used in the same way.
<?php | |
/** | |
* Only display the most expensive shipping rate | |
*/ | |
function my_only_show_most_expensive_shipping_rate( $rates, $package ) { | |
$most_expensive_method = ''; | |
// Loop through shipping rates | |
if ( is_array( $rates ) ) : | |
foreach ( $rates as $key => $rate ) : | |
// Set variables when the rate is more expensive than the one saved | |
if ( empty( $most_expensive_method ) || $rate->cost > $most_expensive_method->cost ) : | |
$most_expensive_method = $rate; | |
endif; | |
endforeach; | |
endif; | |
// Return the most expensive rate when possible | |
if ( ! empty( $most_expensive_method ) ) : | |
return array( $most_expensive_method->id => $most_expensive_method ); | |
endif; | |
return $rates; | |
} | |
add_action( 'woocommerce_package_rates', 'my_only_show_most_expensive_shipping_rate', 10, 2 ); |
Only show free shipping when available
If you offer free shipping rates you may also want to hide the other shipping rates as it doesn’t always make sense to offer paid shipping when there’s also free shipping available (sometimes it is, but it could be that the free shipping/paid shipping are exactly the same).
The following code will only show all the free shipping rates, meaning that if you’ve setup local pickup for example this will still be a option for the user. With the following code only the free shipping rates will show when there is a free shipping available, but it does make sure that it shows the regular shipping rates when local pickup is available (you probably do want to offer the choice to pickup or ship).
<?php | |
/** | |
* Copy from here to your (child) themes functions.php | |
* Recommended to do so via FTP. | |
*/ | |
/** | |
* Hide all but the free shipping options when free is available. | |
* | |
* Shows ALL free options when available. | |
* Does NOT take in account local pickup as free shipping - It does show local pickup rates in the list of free shipping rates | |
* DOES consider non-'free_shipping' rates that have 0 cost as free shipping. | |
*/ | |
function custom_hide_all_shipping_when_free_is_available( $shipping_rates) { | |
$free_rates = array(); | |
$free_shipping_available = false; | |
foreach ( $shipping_rates as $key => $rate ) { | |
// Check for free rates / don't take in account local pickup | |
if ( 0 == $rate->cost && $rate->method_id != 'local_pickup' ) { | |
$free_shipping_available = true; | |
} | |
if ( 0 == $rate->cost ) { | |
$free_rates[ $key ] = $rate; | |
} | |
} | |
// Show all free rates | |
if ( $free_shipping_available ) { | |
return $free_rates; | |
} | |
return $shipping_rates; | |
} | |
add_filter( 'woocommerce_package_rates', 'custom_hide_all_shipping_when_free_is_available' ); |
Closing words
Showing only one correct shipping rate will help your customers with the checkout as they’ve got one less decision to make. These code snippets will hopefully help you show the right shipping rates in your webshop.
Please leave a comment below if this was helpful for you, or if you ran into any challenges 🙂
34 thoughts on “Only show free/cheapest/most expensive shipping in WooCommerce”
Hi, I use your plugin and the advanced pricing add-on.
I have several shipping methods with the same name and want to only show one when there are duplicates available with the same name.
How can this be done?
They are all free ($0.00)
Hi Joe,
If you’d like to do this through the plugin it would depend on your requirements/conditions. Its not possible to automatically hide others with the same name for example.
For this, please reach out through the support channel of the plugin.
In case you’re looking to do this automatically through a customization, thats an option as well. Feel free to reach out if you need help with this.
Cheers,
Jeroen