This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |