Last active
October 7, 2018 17:32
-
-
Save harishankerr/e66fdb208298a511ef091002df206e05 to your computer and use it in GitHub Desktop.
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
/* | |
This code snippet reorders the shipping methods in your WooCommerce website, | |
in an ascending order in terms of shipping costs. | |
Copy the content to your theme's functions.php file or use a custom code snippet plugin | |
like this one: https://github.com/woocommerce/theme-customisations to prevent the code | |
from being overwritten during theme updates. | |
*/ | |
add_filter( 'woocommerce_package_rates' , 'reorder_shipping_methods', 10, 2 ); | |
function reorder_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