I currently charge a flat fee for shipping using the default WooCommerce shipping setup. I want to offer free shipping for the entire order if the user purchases "x" products from a single category. I have some code that I put together but I need a bit of help to get it working.
// Free shipping if you purchase 12 or more from selected category
function wcs_my_free_shipping( $is_available ) {
global $woocommerce;
// HERE set your product categories in the array (can be IDs, slugs or names)
$categories = array('t-shirts');
// Initializing
$found = false;
$count = 0;
// 1st Loop: get category items count
foreach ( WC()->cart->get_cart() as $cart_item ) {
// If product categories is found
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
$count += $cart_item['quantity'];
}
}
// get cart contents
$cart_items = $woocommerce->cart->get_cart();
// loop through the items looking for one in the eligible array
foreach ( $cart_items as $key => $item ) {
if( in_array( $item['product_id'], $eligible ) ) {
return true;
}
}
if ( $count > 11 ) {
// Apply free shipping
$shipping = 0;
}
}
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'wcs_my_free_shipping', 20 );