Skip to content

Instantly share code, notes, and snippets.

@ChromeOrange
Last active June 17, 2016 09:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChromeOrange/9486596 to your computer and use it in GitHub Desktop.
Save ChromeOrange/9486596 to your computer and use it in GitHub Desktop.
Check the cart for specific shipping classes, remove Free Shipping if they are present
/**
* 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;
}
@LindsayHeydon
Copy link

Thanks so much for this.. I tried on the latest version 2.4.6 and it no longer seems to work. I'm adding an item with a 'Heavy' Shipping Class to the basket. I want to remove the Free Shipping option (applied normally if buying over £10.00) IF the Basket contains a 'Heavy' shipping item along with regular items and the total is over £10.00.

I am using a Variable Product with a Shipping Class so although don't think this would have any effect worth a mention. I apologise if I shouldn't write a comment here. Please delete if not appropriate.

Update... now seems to be working. FYI did three things.
WC>System Status>Tools>Clear Transients. (Checked after and no change).
Cleared Basket - re added items. (Checked after and no change).
Re-Updated the Variable Product with Heavy Shipping Class applied. (This seemed to do it, though think it may have been a combination of all three as would have obviously updated\saved this when originally applying the Shipping Class?)

@Flosisseus
Copy link

Worked great until the update to 2.6.
Did they changed the hooks with the new "shipping zones"-feature?

@LindsayHeydon
Copy link

LindsayHeydon commented Jun 17, 2016

This should help re Woo update to 2.6.

I amended part of the original code above to that as below. I removed the var_dump comment to see on the front end what is used and find the names (note; you'll need to check instance ID as will be different). Then I Unset as normal (though I needed to add two in as I've got free delivery in more than one zone). Hope this helps.

// Developers!: Dump this to see what you can unset // var_dump( $available_methods ); // Unset Free Shipping if necessary if( $freeshipping == false ) { unset( $available_methods['free_shipping:5'], $available_methods['free_shipping:6'] ); }

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