Skip to content

Instantly share code, notes, and snippets.

@bolderelements
Last active July 31, 2019 15:34
Show Gist options
  • Save bolderelements/6f573da831eb103f787de7768faa8dd2 to your computer and use it in GitHub Desktop.
Save bolderelements/6f573da831eb103f787de7768faa8dd2 to your computer and use it in GitHub Desktop.
Hide Shipping Method When Cart Contains Shipping Class
/**
* Hide a free shipping option (instance #7) when the given shipping class is in the cart
* Code snippets should be added to the functions.php file of your child theme
*
* @return array
*/
function hide_shipping_when_class_is_in_cart( $rates, $package ) {
// shipping class IDs that need the method removed
$shipping_classes = array('bulky-items');
$if_exists = false;
foreach( $package['contents'] as $key => $values ) {
if( in_array( $values[ 'data' ]->get_shipping_class(), $shipping_classes ) )
$if_exists = true;
}
if( $if_exists ) unset( $rates['free_shipping:7'] );
return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_class_is_in_cart', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment