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

How to Hide Selected Shipping Methods When Free Shipping is Available in WooCommerce?

At times, you may desire to modify the display of shipping methods, for example, hiding shipping method ‘B’ when shipping method ‘A’ is available. The following code allows customers to show free express delivery and hide the paid express delivery option, only when the free shipping option is available. If the free express delivery is unavailable, the alternative shipping method will be displayed.

add_filter( 'woocommerce_package_rates', 'ts_customizing_shipping_methods', 10, 2 );
function ts_customizing_shipping_methods( $rates, $package ) {
    $flat_express_delivery_rate_id = 'flat_rate:4'; // Paid delivery by express mail (cost $35)
    $free_express_delivery_rate_id = 'free_shipping:5'; // Free delivery by express mail
    
    // When Free delivery by express mail is available
    if( isset($rates[$free_express_delivery_rate_id]) ) {
        // Remove Paid delivery by express mail (cost $35)
        unset($rates[$flat_express_delivery_rate_id]);
    }
    return $rates;
}

Output

Consider that the shipping methods present in the below image are the available shipping options.

How to Hide Selected Shipping Methods When Free Shipping is Available in WooCommerce? - Tyche Softwares

If the free express delivery method is present in the $rates array, it removes (hides) the paid express delivery method.

How to Hide Selected Shipping Methods When Free Shipping is Available in WooCommerce? - Tyche Softwares

Now, let’s assume that free shipping is not one of the available shipping options.

How to Hide Selected Shipping Methods When Free Shipping is Available in WooCommerce? - Tyche Softwares

Whenever the free shipping option is not available, only then the paid delivery express mail will be shown in the shipping methods.

How to Hide Selected Shipping Methods When Free Shipping is Available in WooCommerce? - Tyche Softwares

Here we are hiding selective shipping method when free shipping is available. But you can also refer to this post that will hide all other shipping methods when free shipping is available which covers different scenarios in hiding shipping methods.

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