Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save codeagencybe/f7ee93916cfef12eb0d555476a16b632 to your computer and use it in GitHub Desktop.
Save codeagencybe/f7ee93916cfef12eb0d555476a16b632 to your computer and use it in GitHub Desktop.
hide free shipping if shipping class is in cart
/**
* @snippet Disable free shipping option if a specific shipping class is added to cart
* @author Fabio Tielen // Code Agency // https://codeagency.be
* @testedwith WooCommerce 3.2+
*/
add_filter( 'woocommerce_package_rates', 'ca_hide_free_shipping_for_shipping_class', 10, 2 );
function ca_hide_free_shipping_for_shipping_class( $rates, $package ) {
$shipping_class_target = XXX; // search for shipping class ID in your settings
$in_cart = false;
foreach( WC()->cart->cart_contents as $key => $values ) {
if( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
$in_cart = true;
break;
}
}
if( $in_cart ) {
unset( $rates['free_shipping:YYY'] ); // search for the shipping method ID in your settings,
}
return $rates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment