WooCommerce has plenty of shipping options. You can use USPS, FedEx, UPS, flat rate shipping, free shipping, or even create your own table rates. Lots and lots of options for you. If you want to give your customer the choice between these options it can be a bit difficult. WooCommerce will lump the different services together which is nice if you want to see all the USPS, FedEx, UPS, etc. options together. But it doesn't help you if you want to sort by cost and that's how most users want to see things. Either by cost or by delivery time. With a bit of code you can sort the options by cost.
Sort by Cost
If you want to sort all your options by cost then you need to use a tincy snippet which you can add to your theme's functions.php.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// credit: ChromeOrange – https://gist.github.com/ChromeOrange/10013862 | |
add_filter( 'woocommerce_package_rates' , 'patricks_sort_woocommerce_available_shipping_methods', 10, 2 ); | |
function patricks_sort_woocommerce_available_shipping_methods( $rates, $package ) { | |
// if there are no rates don't do anything | |
if ( ! $rates ) { | |
return; | |
} | |
// get an array of prices | |
$prices = array(); | |
foreach( $rates as $rate ) { | |
$prices[] = $rate->cost; | |
} | |
// use the prices to sort the rates | |
array_multisort( $prices, $rates ); | |
// return the rates | |
return $rates; | |
} |
Once you do this all the shipping methods will be sorted by cost from lowest to highest. This example only shows you 3 different methods but with USPS, free shipping, and flat rate shipping it could be a dozen different options.
Make it Understandable
Since all of the options will be mixed up it's worth going into the settings and making it a bit easier to read. With any of the plugins from WooThemes you can go into the settings and change the names. For USPS you go to WooCommerce -> Settings -> Shipping -> USPS and then change the names.
Happy shipping!
Hi Patrick,
Great series of posts, thanks!
I have a question about the display of delivery times through the Canada Post API. If I edit the name of the shipping method, it doesn’t return the delivery estimates.
Is there a way to overcome this?
great snippet, I’m sure its saved be hours of tracing thru code. Thanks Patrick.
Hey Patrick,
Awesome little piece of code here. Would you by chance be able to add something to this code so the default shipping option is the cheapest one? Currently in the cart it selects the second option for me automatically.
Thanks a bunch in advance!
Cheers,
Jacob
In case you’ve not found a solution yet, I found one today.
put into functions.php to make WooCommerce cart default to choosing the cheapest shipping option:
https://gist.github.com/pjv/396dbbbf4d420a15ba88e97ce91b7f34
I too am interested in modifying the snippet so that the default shipping selection is always the least expensive for the customer.
The snippet sorted them correctly, however the default on my site seems to always be the most expensive.
Would you please provide the modified code so that customers are provided the best selection by default?
Kindly, Decli
I found the solution to have WooCommerce cart default to choosing the cheapest shipping option. You can find the code here: https://gist.github.com/pjv/396dbbbf4d420a15ba88e97ce91b7f34
Dear Patrck,
which should be the code to invert this? so I want to display from the most expensive to cheaper option.
thank you!
Stefano
Thank you for the code, wporks perfectly on Aus Post Shipping and woocommerce table rates
hello, this breaks my site and tells me an unexpected error
https://katybarrelcompany.com
is there anything else i can do?
Just wanted to say thinks for providing this bit of code. This is a usability issue that was sticking in our sides and needed a quick solution. We really appreciate folks like yourself who share your knowledge so freely.
Best –
Jerry Norsdtrom
Lead Discovery
Worked great! thanks for the code!
FYI I had to remove the beginning “<?php" tag to make it work
Still works perfectly (you need to refresh the cart by adding a new product or removing one to see change) thx a lot !