In Woocommerce, I want to offer free shipping based on the number of cart items. First, I began looking at the available plugins and I can't find any simple solution based on quantity.
All I want to do is: buy 2 of anything and get free shipping.
Messing around, I tried the following code:
function free_ship( $is_available ) {
$count = 0;
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item) {
$count++;
}
echo $count;
if ( $count == 1 ) {
echo 'add one more for free shipping';
return $is_available;
} elseif ($count > 1) {
echo 'you get free shipping';
return false;
} else {
echo 'nothing in your cart';
return $is_available;
}
}
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'free_ship' );
But it hangs when adding items to the cart. It also is buggy when removing things from the cart. I'd like to figure this out in PHP, so that I can further add more unique conditions in they happen to pop up in the future.
Have any suggestions?