Skip to content

Instantly share code, notes, and snippets.

@harishankerr
Last active October 7, 2018 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harishankerr/e66fdb208298a511ef091002df206e05 to your computer and use it in GitHub Desktop.
Save harishankerr/e66fdb208298a511ef091002df206e05 to your computer and use it in GitHub Desktop.
/*
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