Last active
June 26, 2020 15:59
-
-
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/
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
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 !