In WooCommerce, is it possible to automatically apply free shipping to any product that is on sale?
Every month we have different products on sale, and all sale products automatically qualify for free shipping. For sale products I currently have to manually change the shipping class to "free shipping" and then back to "standard shipping" when the sale is over. I would like to automate this so any product that is on sale automatically qualifies the order for free shipping.
I can apply free shipping per product ID, but I have not been able to figure out applying this to sale products.
function wcs_my_free_shipping( $is_available ) {
global $woocommerce;
// set the product ids that are eligible
$eligible = array( '360' );
// 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;
}
}
// nothing found return the default value
return $is_available;
}
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'wcs_my_free_shipping', 20 );