Created
February 24, 2019 23:04
-
-
Save PierreNodles/c96e485431360f0c6dd6d498a7ad10a4 to your computer and use it in GitHub Desktop.
Activate the free shipping option only when the products in the cart all contain the same specific shipping class
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 | |
// Replace 'shippingClass' with the specific class upon which you wish to make the free shipping option available | |
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 ) { | |
$cart_items = WC()->cart->get_cart(); | |
$found = false; | |
foreach ( $cart_items as $cart_item ) { | |
$product = $cart_item['data']; | |
$class = $product->get_shipping_class(); | |
if ( 'shippingClass' == $class ) { | |
$found = true; | |
} else { | |
$other_item = true; | |
} | |
} | |
if ( $other_item == true) { | |
$found = false; | |
} | |
return $is_available && $found; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment