Created
September 18, 2017 15:44
-
-
Save ccurtin/7f55a220c3d534d2a1dc28fd26b7cf8d to your computer and use it in GitHub Desktop.
Sort the woocommerce_package_rates from lowest to highest
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
<?php | |
// credit: ChromeOrange - https://gist.github.com/ChromeOrange/10013862 | |
add_filter( 'woocommerce_package_rates' , 'sort_woocommerce_available_shipping_methods', 10, 2 ); | |
function sort_woocommerce_available_shipping_methods( $rates, $package ) { | |
// if there are no rates don't do anything | |
if ( ! $rates ) { | |
return; | |
} | |
// get an array of prices | |
$prices = array(); | |
foreach( $rates as $rate ) { | |
$prices[] = $rate->cost; | |
} | |
// use the prices to sort the rates | |
array_multisort( $prices, $rates ); | |
// return the rates | |
return $rates; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment