Skip to content

Instantly share code, notes, and snippets.

@mrfoxtalbot
Forked from mikejolley/gist:11171530
Last active August 29, 2015 14:26
Show Gist options
  • Save mrfoxtalbot/755c7a3733443de04e45 to your computer and use it in GitHub Desktop.
Save mrfoxtalbot/755c7a3733443de04e45 to your computer and use it in GitHub Desktop.
Hide all/some shipping options when free shipping is available
/**
* woocommerce_package_rates is a 2.1+ hook
*/
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
/**
* Hide shipping rates when free shipping is available
*
* @param array $rates Array of rates found for the package
* @param array $package The package array/object being shipped
* @return array of modified rates
*/
function hide_shipping_when_free_is_available( $rates, $package ) {
// Only modify rates if free_shipping is present
if ( isset( $rates['free_shipping'] ) ) {
// To unset all methods except for free_shipping, do the following
$free_shipping = $rates['free_shipping'];
$rates = array();
$rates['free_shipping'] = $free_shipping;
}
return $rates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment