WooCommerce allows anyone to sort their shipping rates through the Shipping Zones settings. Unfortunately its not always possible to all the shipping rates as some may not be available directly from within the shipping zones. For example, multiple shipping rates from live carrier quotes can be retrieved through an API, and usually are setup with only one shipping rate.

In this post I’ll show how you can easily sort the shipping rates by their cost from low to high and high to low with a small code snippet.

When deciding how you want to sort your shipping rates, keep in mind that WooCommerce uses the shipping option at the top as the default selected shipping rate. 

Sorting Shipping Rates from Low to High

The following code snippet will automatically sort all the shipping rates from low to high.

<?php // For implementation instructions see: https://aceplugins.com/how-to-add-a-code-snippet/
/**
* Sort the WC Shipping rates from low to high
*/
function ace_sort_woocommerce_available_shipping_methods( $rates, $package ) {
uasort( $rates, function($a, $b) {
return $b->get_cost() < $a->get_cost();
});
return $rates;
}
add_filter( 'woocommerce_package_rates' , 'ace_sort_woocommerce_available_shipping_methods', 10, 2 );

With this small variation on that script it will only sort the shipping rates from a specific shipping method (say UPS). This way you can keep your custom sorting of the available rates in the shipping zones section, but still sort the shipping carrier rates.

<?php // For implementation instructions see: https://aceplugins.com/how-to-add-a-code-snippet/
/**
* Sort the WC Shipping rates from low to high.
* ONLY for UPS rates
*/
function ace_sort_woocommerce_available_shipping_methods( $rates, $package ) {
uasort( $rates, function($a, $b) {
return $a->get_method_id() == 'ups' && $b->get_method_id() == 'ups' && $b->get_cost() < $a->get_cost();
});
return $rates;
}
add_filter( 'woocommerce_package_rates' , 'ace_sort_woocommerce_available_shipping_methods', 10, 2 );

Sorting Shipping Rates from High to Low

This snippet sorts all the shipping rates from high to low cost. 

<?php // For implementation instructions see: https://aceplugins.com/how-to-add-a-code-snippet/
/**
* Sort the WC Shipping rates from high to low.
* ONLY for UPS rates
*/
function ace_sort_woocommerce_available_shipping_methods( $rates, $package ) {
uasort( $rates, function($a, $b) {
return $a->get_method_id() == 'ups' && $b->get_method_id() == 'ups' && $b->get_cost() > $a->get_cost();
});
return $rates;
}
add_filter( 'woocommerce_package_rates' , 'ace_sort_woocommerce_available_shipping_methods', 10, 2 );

Lastly same as the low to high option this will sort only the shipping rates of the UPS shipping method from high to low. 

<?php // For implementation instructions see: https://aceplugins.com/how-to-add-a-code-snippet/
/**
* Sort WooCommerce shipping methods by cost, highest to lowest
*/
function ace_sort_woocommerce_available_shipping_methods( $rates, $package ) {
uasort( $rates, function($a, $b) {
return $b->get_cost() > $a->get_cost();
});
return $rates;
}
add_filter( 'woocommerce_package_rates' , 'ace_sort_woocommerce_available_shipping_methods', 10, 2 );

About the author: Jeroen Sormani is actively building WordPress, WooCommerce and Easy Digital Downloads plugins. Slightly obsessed by writing high quality code.
Follow Jeroen on Twitter

Leave a Reply

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