3
votes

There are lots of ways to hide shipping method when free shipping is active.
My problem is that I am always offer free shipping on every order on the site.
And at the same time i also offers a "faster" delivery that Requiring additional payment.
That's why the codes on the Internet did not help me in this case.
So How is it possible to hide a specific shipping method when another specific shipping method appears.
I will give an example of this.

For orders up to 350$, there are 2 shipping options on the website:
a. Free shipping by regular mail
b. Delivery by express mail for a fee of 35 $

When ordering over 350, there are 3 options.
a. Free shipping by regular mail
b. Delivery by express mail for a fee of 35 $
c. Free shipping by express mail.

In this example I want to hide shipment b when shipment c appears.
I searched a lot and did not find any way to do it. Any help is appreciated.

1

1 Answers

2
votes

Updated - added a missing ;

First you need to get the correct shipping rate Ids for "Delivery by express mail ($35)" and also for "Free shipping by express mail".

This can be do when inspecting the generated html code (with your browser inspector) on your shipping methods radio buttons. so you will see something like:

enter image description here

where the shipping method rate Id is free_shipping:10 (the radio button "value").

The code below will hide "Paid delivery by express mail (cost $35)" when "Free delivery by express mail" is available $(you will have to set both shipping rate Ids)*:

add_filter( 'woocommerce_package_rates', 'customizing_shipping_methods', 10, 2 );
function customizing_shipping_methods( $rates, $package ) {
    $flat_express_delivery_rate_id = 'flat_rate:12'; // Paid delivery by express mail (cost $35)
    $free_express_delivery_rate_id = 'free_shipping:10'; // 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;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Refresh shipping methods

Once you have saved this code, empty cart to refresh shipping methods, or if needed, go to shipping areas settings, then disable / save and re-enable / save any shipping methods from a shipping zone.