Skip to content

Instantly share code, notes, and snippets.

@Savantos
Forked from ChromeOrange/gist:9559252
Created September 15, 2016 16:52
Show Gist options
  • Save Savantos/96d01fb6d0c6d3a3912e4f36f4029b9c to your computer and use it in GitHub Desktop.
Save Savantos/96d01fb6d0c6d3a3912e4f36f4029b9c 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment