Skip to content

Instantly share code, notes, and snippets.

@xadapter
Last active June 26, 2020 15:59
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 xadapter/2335a13dacef1472b803ef18cc39f2d9 to your computer and use it in GitHub Desktop.
Save xadapter/2335a13dacef1472b803ef18cc39f2d9 to your computer and use it in GitHub Desktop.
Snippet to sort WooCommerce shipping services by cost. Supports PluginHive Shipping Plugins: https://www.pluginhive.com/product-category/woocommerce-plugin/woocommerce-shipping/
add_filter( 'woocommerce_package_rates' , 'xa_sort_shipping_services_by_cost', 10, 2 );
function xa_sort_shipping_services_by_cost( $rates, $package ) {
if ( ! $rates ) return;
$rate_cost = array();
foreach( $rates as $rate ) {
$rate_cost[] = $rate->cost;
}
// using rate_cost, sort rates.
array_multisort( $rate_cost, $rates );
return $rates;
}
@dydadou
Copy link

dydadou commented Jun 26, 2020

Hi,
I'm facing an issue while using this code : we don't want to show shipping costs until the client fill a valid address.
But when we enable "Hide shipping costs until an address is entered" in woocommerce shipping options, and use this snippet in the same time, then the usual message "Enter your full address to see shipping costs" disappear and our clients are lost.
Is there a way to put it back ?
Many thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment