Skip to content

Instantly share code, notes, and snippets.

@bolderelements
Last active September 25, 2017 18:49
Show Gist options
  • Save bolderelements/f5f866744f4d523b0e20fafd6d2dd0c1 to your computer and use it in GitHub Desktop.
Save bolderelements/f5f866744f4d523b0e20fafd6d2dd0c1 to your computer and use it in GitHub Desktop.
Hide All Free Shipping Options When Cart Contains a Shipping Class
/**
* Hide all free shipping options 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 ) {
foreach( $rates as $key => $option ) {
if( $option->method_id == 'free_shipping' )
unset( $rates[ $key ] );
}
}
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