Skip to content

Instantly share code, notes, and snippets.

@ChromeOrange
Created March 14, 2014 23:33
Show Gist options
  • Save ChromeOrange/9559252 to your computer and use it in GitHub Desktop.
Save ChromeOrange/9559252 to your computer and use it in GitHub Desktop.
Deactivate Flat Rate Shipping if products with specific shipping classes are in the cart
/**
* Deactivate Flat Rate Shipping if products with specific shipping
* classes are in the cart
*
* Add the shipping class slugs to the $shippingclass_array array
*/
add_filter( 'woocommerce_shipping_flat_rate_is_available', 'unset_woocommerce_shipping_methods_flat_rate', 10 ,2 );
function unset_woocommerce_shipping_methods_flat_rate ( $return, $package ) {
// Setup an array of shipping classes that do not allow Flat Rate Shipping
$shippingclass_array = array( 'pickup-only' );
// loop through the cart checking the shipping classes
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$shipping_class = get_the_terms( $values['product_id'], 'product_shipping_class' );
if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, $shippingclass_array ) ) {
/**
* If a product in the cart has a shipping class that does not allow for Flat Rate Shipping
* then return false to unset the Flat Rate shipping method and break, no need to carry on
*/
return false;
break;
}
}
/**
* It we make it this far then
* Flat Rate must be available, return true
*/
return true;
}
@grantmccall
Copy link

Hello, is it possible to use this function to remove an "Additional rate" that is definfed within flat rate shipping?
For example, I have flat rate shipping of $10 - then I have set an additional rate called "Express Shipping" of $20 per item for express shipping.
I need to remove this additional rate if there are certain shipping classes in the cart. Is this possible?
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment