WooCommerce: Sort Shipping Costs from Low to High

A client had several shipping rates on the cart page automatically generated by FedEx, USPS, UPS and similar plugins via their API. Problem was, they wanted to sort them by price as opposed to grouping them by provider.

Thankfully, with a simple “uasort” PHP function, it’s possible to take the shipping rates array and sort it by amount before returning it back to the screen. If you don’t know PHP, simply copy/paste!

Sort shipping costs from low to high in WooCommerce
Sort shipping costs from low to high in WooCommerce

PHP snippet: Sort Shipping Rates by Price @ WooCommerce Cart/Checkout

 * @snippet       Sort Shipping Rates by Price - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    Woo 3.8
 * @donate $9     https://businessbloomer.com/bloomer-armada/
add_filter( 'woocommerce_package_rates' , 'businessbloomer_sort_shipping_methods', 10, 2 );
function businessbloomer_sort_shipping_methods( $rates, $package ) {
    if ( empty( $rates ) ) return;
    if ( ! is_array( $rates ) ) return;
    uasort( $rates, function ( $a, $b ) { 
        if ( $a == $b ) return 0;
        return ( $a->cost < $b->cost ) ? -1 : 1; 
    } );
    return $rates;

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7+.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce Customization?

Check out these free video tutorials. You can start learning how to customize WooCommerce without unnecessary plugins. Watch me code and learn by example!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

22 thoughts on “WooCommerce: Sort Shipping Costs from Low to High

  1. Thanks, this worked great with no modification on my site. However, I would like to sort shipping methods according to a specific list that I believe the customer prefer (the most popular alternatives at the top), or at least be able to put one option (Pick up in store) at the end of the list. How would i go about doing that?

    1. Hi Soren, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. It’s possible to order from high to low? Thanks.

    1. Of course – just play with the uasort function

  3. Hi… this code that worked great at first but we have 2 different types of free shipping and it’s only sorting one of them to the top… any idea on how to fix that? Here is an example of what I mean…. if you add any nappies to the basket that total over $99 free shipping is listed and is shown first just as should be – Green Kids Free Shipping.
    BUT… if you add drytimes incontinence products to the basket that total over $29 (free shipping threshold for that product line – make sure no nappies are in the cart or free shipping doesn’t turn on) and the Dry Times Free Shipping does not appear first but gets pushed back to last again. Any ideas why??

    1. Hello Amanda, thanks for your comment ๐Ÿ™‚ Are you using a custom plugin for free shipping?

  4. Even with your January 10 comment and update, my error log is still full of this error as a result of this code:

    [12-Mar-2019 22:54:34 UTC] PHP Warning: array_keys() expects parameter 1 to be array, null given in /home/account/public_html/wp-content/plugins/woocommerce/includes/wc-cart-functions.php on line 468
    [12-Mar-2019 22:54:34 UTC] PHP Warning: array_keys() expects parameter 1 to be array, null given in /home/account/public_html/wp-content/plugins/woocommerce/includes/wc-cart-functions.php on line 439
    [12-Mar-2019 22:54:34 UTC] PHP Warning: current() expects parameter 1 to be array, null given in /home/account/public_html/wp-content/plugins/woocommerce/includes/wc-cart-functions.php on line 440

    Any ideas?

    1. Hey Nathan, I’ve revised the snippet. Can you try this version please?

    2. I tried that revised snippet and unfortunately there’s no difference.

      1. Sure it’s my snippet? Those 2 functions are not “directly” inside it, unless they are called from somewhere else

  5. It breaks some stores because it resets the indexes

    replace with

    uasort($rates, function ($a, $b) { return $a->cost – $b->cost; });
    return $rates;

    1. Thanks for that Chris, much appreciated!

    2. @Chris Awesome you’re the man. This was problem on my store when sorting shipping options. You’d select shipping option from /cart page, it would appear to make ajax call and then it would deselect option & total would not get updated. uasort did the trick. Thank you!

  6. Hi Rodolfo,

    Your snippet does what it is supposed to. However, in the cart, I get these warnings:

    Warning: array_keys() expects parameter 1 to be array, null given in /home/ballsnba/public_html/wp-content/plugins/woocommerce/includes/wc-cart-functions.php on line 465

    Warning: current() expects parameter 1 to be array, null given in /home/ballsnba/public_html/wp-content/plugins/woocommerce/includes/wc-cart-functions.php on line 437

    Warning: array_keys() expects parameter 1 to be array, null given in /home/ballsnba/public_html/wp-content/plugins/woocommerce/includes/wc-cart-functions.php on line 465

    Warning: current() expects parameter 1 to be array, null given in /home/ballsnba/public_html/wp-content/plugins/woocommerce/includes/wc-cart-functions.php on line 437

    This doesn’t bother me except I don’t want my customers to see this! Any ideas? (Caveat: I know nothing about coding; I just paste what I am given into relevant files.)

    Thanks for your help.


    1. Hello Richard, thanks for your comment! First of all, those error messages should be hidden – please use this instead: https://codex.wordpress.org/Debugging_in_WordPress#Example_wp-config.php_for_Debugging

      In regard to the errors themselves, it could be you only have one shipping method in certain cases, and therefore you have no array. I’ve now added a new line to the snippet (is_array) to avoid this – let me know if those errors go away ๐Ÿ™‚

  7. Thanks for the snippet. I tried yours and several other people’s snippets. I could not get my shipping to sort from low to high.

    Then I saw this and did it. Now it works! Great! and thanks for the snippet. ๐Ÿ™‚
    Clear Customer Sessions” via WooCommerce > Status > Tools

    1. Excellent ๐Ÿ™‚

  8. Hi Rodolfo,

    It works now. Just cleared the cache and cart items. Thanks and Sorry

  9. Hello Rodolfo,

    Thanks for the hook. I added this function into my theme’s functions.php but its not showing the price from low to high. I am using WooCommerce 3.2.6. Any idea?


  10. Heads up… this seems to break “WooCommerce Advanced Shipping” with WP 4.9.4 (and possibly earlier)

    Not sure why but developer informed.

    1. Hi Martin, thanks for that! My snippets work for default WooCommerce and might not be compatible with third party plugins, so not sure there I’m afraid. I just retested this snippet in Woo 3.3.3 and it still works ๐Ÿ™‚

Questions? Feedback? Support? Leave your Comment Now!

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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