Skip to content

Instantly share code, notes, and snippets.

@BFTrick
Created June 1, 2016 16:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BFTrick/bf73798cc1562e1214e4c0f9cc3dd0fd to your computer and use it in GitHub Desktop.
Save BFTrick/bf73798cc1562e1214e4c0f9cc3dd0fd to your computer and use it in GitHub Desktop.
Sort WooCommerce shipping methods by cost
<?php
// credit: ChromeOrange - https://gist.github.com/ChromeOrange/10013862
add_filter( 'woocommerce_package_rates' , 'patricks_sort_woocommerce_available_shipping_methods', 10, 2 );
function patricks_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