Skip to content

Instantly share code, notes, and snippets.

@mikejolley
Created May 24, 2016 10:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikejolley/2721533c73d5d517cc822bf632297310 to your computer and use it in GitHub Desktop.
Save mikejolley/2721533c73d5d517cc822bf632297310 to your computer and use it in GitHub Desktop.
WooCommerce - enable free shipping only if product class is found in cart.
<?php // Do not include this if already open!
/**
* Code goes in theme functions.php. Free Shipping Method must be enabled.
*/
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'free_shipping_based_on_cart_shipping_class' );
function free_shipping_based_on_cart_shipping_class( $is_available ) {
/**
* This example enables free shipping only when an item is found in the cart with a class named 'free_shipping'
*/
$cart_items = WC()->cart->get_cart();
$found = false;
foreach ( $cart_items as $cart_item ) {
$product = $cart_item['data'];
$class = $product->get_shipping_class();
if ( 'free_shipping' === $class ) {
$found = true;
break;
}
}
return $is_available && $found;
}
Copy link

ghost commented Apr 12, 2017

Hello, I've added the code into my functions.php file and it is not working.

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