Last active
December 16, 2022 18:24
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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;