Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Enable Free Shipping for Products on Sale in WooCommerce?

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.

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.

How to Enable Free Shipping for Products on Sale in WooCommerce?

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.

How to Enable Free Shipping for Products on Sale in WooCommerce?


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.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible.

Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

By using our site, you acknowledge that you have read and understood our Privacy Policy and Terms & Conditions.