/** * Check the cart for specific shipping classes, remove Free Shipping if they are present * * This code will remove free shipping if a product is in the cart that can not be delivered for free * Assign a shipping class to those products and add the slug to the $freeshipping_array * Multiple shipping classes can be added to the array * * Add the code to your theme functions.php file */ add_filter( 'woocommerce_package_rates', 'unset_free_shipping_when_product_in_cart' , 10, 1 ); function unset_free_shipping_when_product_in_cart( $available_methods ) { /** * Set free shipping variable to true * * Free shipping is allowed unless we find a product with a shipping class that * does not allow free shipping */ $freeshipping = true; // Setup an array or shipping classes that do not allow free shipping $freeshipping_array = array( 'local-delivery' ); // 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( in_array( $shipping_class[0]->slug, $freeshipping_array ) && $freeshipping ) { $freeshipping = false; break; } } // Unset Free Shipping if necessary if( $freeshipping == false ) { unset( $available_methods['free_shipping'] ); } return $available_methods; }