Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Hide shipping rates when free shipping is available.
* Updated to support WooCommerce 2.6 Shipping Zones.
*
* @param array $rates Array of rates found for the package.
* @return array
*/
function my_hide_shipping_when_free_is_available( $rates ) {
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );
@LukeBronsky

This comment has been minimized.

Copy link

@LukeBronsky LukeBronsky commented Jan 13, 2017

Any idea how to make this code work when I set 2 free shipping methods and apply this code, it only shows the first free shipping method and not the second one?

@arigato-one

This comment has been minimized.

Copy link

@arigato-one arigato-one commented May 18, 2017

Any plans to update this for WooCommerce 3.x ?

@lukecav

This comment has been minimized.

Copy link

@lukecav lukecav commented Jan 15, 2018

So you can use this with tiered shipping, so in the example two flat rates and one free shipping rate. So either of the flat rates would show depending on the threshold and then if the minimum order amount is set on the free shipping method, only the free shipping method would show and not both.

https://businessbloomer.com/woocommerce-setup-tiered-shipping-rates-order-amount/

@Yizi

This comment has been minimized.

Copy link

@Yizi Yizi commented Mar 6, 2018

Any ideas how we can add a clause to show another shipping method along the way e.g. Special Delivery. So if the customer is eligible for the FREE shipping they will also see the option for Special Delivery just in case they need it faster...

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