Do you want to provide free shipping only for on-sale products?
This code shows a free shipping notice for products on sale, hides other shipping options, and only displays free shipping on both the cart and checkout pages.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function ts_filter_woocommerce_shipping_free_shipping_is_available( $is_available, $package, $shipping_method ) {
// Loop through cart items
foreach ( $package['contents'] as $cart_item ) {
// On sale
if ( $cart_item['data']->is_on_sale() ) {
// True
$is_available = true;
// Notice
$notice = __( 'Free shipping is enabled for products on sale.', 'woocommerce' );
// Break loop
break;
}
}
// Display notice
if ( isset( $notice ) ) {
wc_add_notice( $notice, 'notice' );
}
return $is_available;
}
// Hide other shipping methods when free shipping is available
function ts_hide_shipping_when_free_is_available( $rates ) {
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->get_method_id() ) {
$free[ $rate_id ] = $rate;
}
}
return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'ts_hide_shipping_when_free_is_available', 100 );
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'ts_filter_woocommerce_shipping_free_shipping_is_available', 10, 3 );
function ts_filter_woocommerce_shipping_free_shipping_is_available( $is_available, $package, $shipping_method ) {
// Loop through cart items
foreach ( $package['contents'] as $cart_item ) {
// On sale
if ( $cart_item['data']->is_on_sale() ) {
// True
$is_available = true;
// Notice
$notice = __( 'Free shipping is enabled for products on sale.', 'woocommerce' );
// Break loop
break;
}
}
// Display notice
if ( isset( $notice ) ) {
wc_add_notice( $notice, 'notice' );
}
return $is_available;
}
// Hide other shipping methods when free shipping is available
function ts_hide_shipping_when_free_is_available( $rates ) {
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->get_method_id() ) {
$free[ $rate_id ] = $rate;
}
}
return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'ts_hide_shipping_when_free_is_available', 100 );
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'ts_filter_woocommerce_shipping_free_shipping_is_available', 10, 3 );
function ts_filter_woocommerce_shipping_free_shipping_is_available( $is_available, $package, $shipping_method ) { // Loop through cart items foreach ( $package['contents'] as $cart_item ) { // On sale if ( $cart_item['data']->is_on_sale() ) { // True $is_available = true; // Notice $notice = __( 'Free shipping is enabled for products on sale.', 'woocommerce' ); // Break loop break; } } // Display notice if ( isset( $notice ) ) { wc_add_notice( $notice, 'notice' ); } return $is_available; } // Hide other shipping methods when free shipping is available function ts_hide_shipping_when_free_is_available( $rates ) { $free = array(); foreach ( $rates as $rate_id => $rate ) { if ( 'free_shipping' === $rate->get_method_id() ) { $free[ $rate_id ] = $rate; } } return ! empty( $free ) ? $free : $rates; } add_filter( 'woocommerce_package_rates', 'ts_hide_shipping_when_free_is_available', 100 ); add_filter( 'woocommerce_shipping_free_shipping_is_available', 'ts_filter_woocommerce_shipping_free_shipping_is_available', 10, 3 );
Output
Let’s assume that a customer adds an on-sale product to the cart.
When the customer views the cart, the code will display a free shipping notice message only for on-sale products. Also, it hides other shipping options, leaving only free shipping available.
On-sale products are also a sort of providing discounts for products. Similar to this customization, you can also enable the shipping method when a coupon is applied in WooCommerce.