Skip to content

Instantly share code, notes, and snippets.

@ChromeOrange
Last active December 16, 2022 18:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChromeOrange/10013862 to your computer and use it in GitHub Desktop.
Save ChromeOrange/10013862 to your computer and use it in GitHub Desktop.
Sort shipping methods by cost in WooCommerce 2.1. Add to theme functions.php file
/**
* Sort WooCommerce shipping methods by cost, lowest to highest
*/
add_filter( 'woocommerce_package_rates' , 'sort_woocommerce_available_shipping_methods', 10, 2 );
function sort_woocommerce_available_shipping_methods( $rates, $package ) {
if ( !$rates ) return $rates;
$tmp = Array();
foreach( $rates as $ma ) {
$tmp[] = $ma->cost;
}
array_multisort( $tmp, $rates );
return $rates;
}
@NewVibe
Copy link

NewVibe commented Aug 29, 2022

Seeing an error with logged in users that have nothing in their cart yet, both on the cart page and when adding to cart. The solution was to always return $rates, so change the first line to:

if ( !$rates ) return $rates;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment