Last active
August 31, 2018 02:40
-
-
Save bolderelements/f365a0c82b2f5e9de283f50f092a221c to your computer and use it in GitHub Desktop.
Hide a Shipping Option When Another Option is Available (Title Based)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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