Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mlbd/d3f716f5f50efcb982c08e5d5701a275 to your computer and use it in GitHub Desktop.
Save mlbd/d3f716f5f50efcb982c08e5d5701a275 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 allaround_hide_shipping_when_free_is_available( $rates ) {
// Here your free shipping rate Id
$free_shipping_rate_id = 'free_shipping:6';
// When your Free shipping method is available
if ( array_key_exists( $free_shipping_rate_id, $rates ) ) {
// Loop through shipping methods rates
foreach ( $rates as $rate_key => $rate ) {
// Removing "Flat rate" shipping method
if ( 'flat_rate' === $rate->method_id ){
unset($rates[$rate_key]);
}
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'allaround_hide_shipping_when_free_is_available', 100 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment