Skip to content

Instantly share code, notes, and snippets.

@SiR-DanieL
Created September 27, 2016 09:29
Show Gist options
  • Save SiR-DanieL/c39f8c3498e6cec810986669c43723b8 to your computer and use it in GitHub Desktop.
Save SiR-DanieL/c39f8c3498e6cec810986669c43723b8 to your computer and use it in GitHub Desktop.
/**
* 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
Copy link

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
Copy link

Any plans to update this for WooCommerce 3.x ?

@lukecav
Copy link

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
Copy link

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