WooCommerce Sort Shipping Methods By Cost

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.

WooCommerce Shipping Costs

The WooCommerce shipping methods in the default order.

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.


<?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;
}

view raw

functions.php

hosted with ❤ by GitHub

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.

WooCommerce Shipping Costs Ordered

WooCommerce shipping methods ordered lowest to highest.

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.

WooCommerc USPS Service Names

The USPS services names. You can type in anything you want. I've added “USPS” to the front of all of mine.

Happy shipping!

13 thoughts on “WooCommerce Sort Shipping Methods By Cost

  1. 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?

  2. great snippet, I’m sure its saved be hours of tracing thru code. Thanks Patrick.

  3. 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

  4. 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

  5. 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

  6. Thank you for the code, wporks perfectly on Aus Post Shipping and woocommerce table rates

  7. hello, this breaks my site and tells me an unexpected error
    https://katybarrelcompany.com

    is there anything else i can do?

  8. 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

  9. Worked great! thanks for the code!

  10. FYI I had to remove the beginning “<?php" tag to make it work

  11. Still works perfectly (you need to refresh the cart by adding a new product or removing one to see change) thx a lot !

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.