Skip to content

Instantly share code, notes, and snippets.

@bolderelements
Last active August 31, 2018 02:40
Show Gist options
  • Save bolderelements/f365a0c82b2f5e9de283f50f092a221c to your computer and use it in GitHub Desktop.
Save bolderelements/f365a0c82b2f5e9de283f50f092a221c to your computer and use it in GitHub Desktop.
Hide a Shipping Option When Another Option is Available (Title Based)
/**
* Hide a shipping option when the given shipping option is in the cart based on their titles
* Code snippets should be added to the functions.php file of your child theme
*
* @return array
*/
function hide_shipping_when_option_is_available( $rates ) {
// shipping option titles to be removed and the requirement, respectively
$option_removed = 'Super Saver Shipping';
$option_needed = 'Free Super Saver Shipping';
$if_exists = false;
foreach( $rates as $key => $option ) {
if( $option->get_label() == $option_needed )
$if_exists = true;
}
if( $if_exists ) {
foreach( $rates as $key => $option ) {
if( $option->get_label() == $option_removed )
unset( $rates[ $key ] );
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_option_is_available', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment