Skip to content

Instantly share code, notes, and snippets.

@munts
Last active August 26, 2020 19:33
Show Gist options
  • Save munts/11e12c2491ae4cd92a7585a9b4a4b174 to your computer and use it in GitHub Desktop.
Save munts/11e12c2491ae4cd92a7585a9b4a4b174 to your computer and use it in GitHub Desktop.
Hide WooCommerce shipping options when free shipping is available. Ie. hide Flat Rate.
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 100 );
function 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;
}
@munts
Copy link
Author

munts commented Aug 26, 2020

Of course, this is primarily valid if the shipping options are Flat Rate and Free Shipping. If you offer standard 3-5 day delivery, Business 2-3 day, and Express Overnight, as well as Free Shipping (5+ days) then you probably don't want to hide all the options.

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