Skip to content

Instantly share code, notes, and snippets.

@woogists
Created July 4, 2018 11:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save woogists/57e4cf73f083190c0e00bd59f060f87d to your computer and use it in GitHub Desktop.
Save woogists/57e4cf73f083190c0e00bd59f060f87d to your computer and use it in GitHub Desktop.
[General Snippets][Hide other shipping methods, but keep "Local pickup" when “Free Shipping” is available]
/**
* Hide shipping rates when free shipping is available, but keep "Local pickup"
* Updated to support WooCommerce 2.6 Shipping Zones
*/
function hide_shipping_when_free_is_available( $rates, $package ) {
$new_rates = array();
foreach ( $rates as $rate_id => $rate ) {
// Only modify rates if free_shipping is present.
if ( 'free_shipping' === $rate->method_id ) {
$new_rates[ $rate_id ] = $rate;
break;
}
}
if ( ! empty( $new_rates ) ) {
//Save local pickup if it's present.
foreach ( $rates as $rate_id => $rate ) {
if ('local_pickup' === $rate->method_id ) {
$new_rates[ $rate_id ] = $rate;
break;
}
}
return $new_rates;
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
@BenAngels
Copy link

Hi, this great snippet always worked fine. However not anymore with the new Woocommerce version Versie 8.6.1 it works not anymore (for me). Can't find if something changed. Anyone an idea to get it working again?

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