WooCommerce doesn’t sort shipping options/services by default while showing it on the Cart and Checkout page. In this guide, we are going to help you sort Shipping methods based on shipping cost on your WooCommerce store.
In order to achieve the business case, you must add in the Code Snippet mentioned below.
Code Snippet to Sort Shipping Options by Cost
Below code snippet will help to sort the shipping options by shipping cost.
add_filter( 'woocommerce_package_rates' , 'ph_sort_shipping_services_by_cost', 10, 2 );
function ph_sort_shipping_services_by_cost( $rates, $package ) {
if ( ! $rates ) return $rates;
$rate_cost = array();
foreach( $rates as $rate ) {
$rate_cost[] = $rate->cost;
}
// using rate_cost, sort rates.
array_multisort( $rate_cost, $rates );
return $rates;
}
Screenshot – Shipping Options – Default view.

Screenshot – Shipping Options – Sorted view.

- WooCommerce UPS Shipping plugin
- WooCommerce FedEx Shipping plugin
- WooCommerce Canada Post Shipping plugin
- WooCommerce Multi-Carrier Shipping plugin
- WooCommerce Table Rate Shipping Pro plugin
Code still works! Thank you!
This used to work up until we upgraded woocommerce to 3.2.5 and wordpress to 4.9.1 now when something is added to the cart, it returns this error:
Warning: array_keys() expects parameter 1 to be array, null given in /home4/ffxbmeus/public_html/wp-content/plugins/woocommerce/includes/wc-cart-functions.php on line 478
Warning: array_keys() expects parameter 1 to be array, null given in /home4/ffxbmeus/public_html/wp-content/plugins/woocommerce/includes/wc-cart-functions.php on line 449
Warning: current() expects parameter 1 to be array, null given in /home4/ffxbmeus/public_html/wp-content/plugins/woocommerce/includes/wc-cart-functions.php on line 450
Warning: array_keys() expects parameter 1 to be array, null given in /home4/ffxbmeus/public_html/wp-content/plugins/woocommerce/includes/wc-cart-functions.php on line 478
Warning: array_keys() expects parameter 1 to be array, null given in /home4/ffxbmeus/public_html/wp-content/plugins/woocommerce/includes/wc-cart-functions.php on line 449
Warning: current() expects parameter 1 to be array, null given in /home4/ffxbmeus/public_html/wp-content/plugins/woocommerce/includes/wc-cart-functions.php on line 450
Hi Marc,
We tested this on our side but could not replicate the issue. Would it be possible for you to approach our support team on this so that they can resolve the problem? Here is the URL of support.
Works Perfectly, Thanks a lot!
You are welcome. 🙂
Thanks!
thats for the code, it works great!
You are welcome. 🙂
Thanks!
Where is the code snippet?
You will find it under the title “Code Snippet for sorting the shipping options by cost.”.
One should be careful with this code snippet. It sorts the shipping correctly, however, it causes an error to appear that says “no shipping method has been selected” when clicking the payment button. I’m using the latest version of wordpress (4.6.1) and woocommerce (2.6.6).
Strange. I can’t see the code snippet. I tried 3 different browsers. I see the title and the screenshots, but no code snippet.
Worked! Thank you for this snippet