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

Hide Other WooCommerce Shipping Methods When Free Shipping is Available

How to hide other WooCommerce shipping methods when Free Shipping is available | tychesoftwares.com

In our earlier post, we have explained how one can hide WooCommerce shipping methods for certain conditions. The most common instance for an eCommerce store would be hiding other shipping methods during checkout when the Free Shipping is available for the order.

This would make more sense to a store owner for customer’s smooth purchase experience. If free shipping is available for an order, most of the customers would prefer to select that only instead of a paid shipping method. So to rather ask them to select Free Shipping among all the shipping methods, it is better to select it automatically for them and hide other methods.

Ways to Hide Other WooCommerce Shipping Methods When Free Shipping is Available

WooCommerce shows by default all the shipping methods for a matching shipping zone, so it is not possible to hide them with the settings. This can be achieved by adding a code snippet or by plugins available.

Default Shipping Methods
Default Shipping Methods

Here, we will take an example where all the shipping methods will be hidden when the Free Shipping option is available for the matching shipping zone. To achieve this, add the below code snippet in the functions.php file of the child theme, or use any code snippet plugin to add this code.

/**
 * Hide shipping methods when free shipping is available.
 *
 * @param array $rates Array of rates found for the package.
 * @return array
 */
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 );

Here the woocommerce_package_rates filter is being used to modify the calculated rates on the cart. It contains all the shipping methods which will be available once the product is added to the cart. So, here we have created an array that will contain all the free shipping methods available. If the array of free shipping is not empty then return that array else return all the shipping methods.

Hide other Shipping rates when Free shipping is available
Hide other Shipping methods when Free shipping is available

Understanding get_method_id()

The code snippet uses the  get_method_id() function to fetch the method ID for each shipping method. In our example, when the get_method_id() function returns ‘free_shipping’, it means that the ‘Free Shipping’ method is selected for the matching shipping zone. The method IDs for other shipping methods are:

  • ‘Local Pickup’: local_pickup
  • ‘Flat Rate’: flat_rate

Thus the above code hides all other shipping options except ‘Free shipping’ irrespective of the shipping zone.

Hiding Specific Shipping Methods for a Zone

To hide shipping options for a particular shipping zone, you need to use the correct method_id in the code (e.g., ‘free_shipping:2’)

For example, if you want to unset the Flat rate for a particular zone, like India, you can right-click on the shipping method during checkout and inspect the element using your browser’s developer tools.

Hide Other WooCommerce Shipping Methods When Free Shipping is Available - Tyche Softwares
Find the instance id of a shipping method
Hide Other WooCommerce Shipping Methods When Free Shipping is Available - Tyche Softwares

By including this ID (flat_rate:15) in the code snippet, you can hide specific shipping methods that belong to a particular shipping zone.

Ways to Hide Other WooCommerce Shipping Methods except Local Pickup & Free Shipping

Some store owners may want to offer Local Pickup option along with Free Shipping method on the checkout page.

The following code snippet is designed to help you achieve this customization.

function ts_hide_shipping_when_free_shipping_available( $rates ) {
    $free_shipping_exists = false;
    $local_pickup_exists = false;

    // Check if "Free Shipping" and "Local Pickup" methods exist
    foreach ( $rates as $rate_key => $rate ) {
        if ( 'free_shipping' === $rate->get_method_id() ) {
            $free_shipping_exists = true;
        }
        if ( 'local_pickup' === $rate->get_method_id() ) {
            $local_pickup_exists = true;
        }
    }

    // Unset all other shipping methods except "Free Shipping" and "Local Pickup"
    if ( $free_shipping_exists && $local_pickup_exists ) {
        foreach ( $rates as $rate_key => $rate ) {
            if ( 'free_shipping' !== $rate->get_method_id() && 'local_pickup' !== $rate->get_method_id() ) {
                unset( $rates[ $rate_key ] );
            }
        }
    }

    return $rates;
}
add_filter( 'woocommerce_package_rates', 'ts_hide_shipping_when_free_shipping_available', 100 );

Output

The output hereby depicts that the code snippet has modified the default shipping methods available on the checkout page. Specifically, it hides all shipping methods except for “Free Shipping” and “Local Pickup” if both of these options are available for the current order.

Hide Other WooCommerce Shipping Methods When Free Shipping is Available - Tyche Softwares
Shipping options while code is active

The following output shows all the shipping methods as the code is not implemented.

Hide Other WooCommerce Shipping Methods When Free Shipping is Available - Tyche Softwares
Shipping options while code is Inactive

2. Using Extensions for WooCommerce

There are multiple extensions available which provides an option to hide other shipping options when free shipping is available. Below are some extensions listed:

1. WC Hide Shipping Methods

This plugin automatically hides all other shipping methods when “free shipping” is available during the checkout process. It also includes an option to keep “local pickup” available alongside “free shipping”.

2. Hide Shipping Method For WooCommerce

A new free plugin released a few months back also allows you to hide other shipping methods when free shipping is available.

3. ELEX Hide WooCommerce Shipping Methods

This is a paid plugin that has a feature of hiding other shipping methods when free shipping available. It also has many other options like hiding shipping methods using filters like WooCommerce products, shipping classes. etc.

4. Hide Shipping Method Plugin

It’s very easy to use the Hide Shipping Method Plugin to hide one, several, or all shipping methods for a WooCommerce cart. The plugin lets you:

  • Hide all other shipping methods in the case of Free Shipping
  • Hide all other shipping methods in the case of “Free Shipping” and/or “Local Pickup”
  • Hide selected shipping methods in the case of Free Shipping
  • Hide selected shipping methods as per conditional Hide shipping method Rules

Conclusion

Hiding unwanted options while purchasing makes the checkout & overall purchase experience more smooth and clearer to the customer.

Try out the above code and let us know. 🙂

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

Share It:

Subscribe
Notify of
7 Comments
Newest
Oldest
Inline Feedbacks
View all comments
1 year ago

Hi there

Thanks for most helpful article, mad why this isn’t defaulted, but has there been a WooCommerce update that stops this from working now ?

2 years ago

Your code is great, as there is a typo in the original WooCommerce docs, for the same kind of example.
https://woocommerce.com/document/hide-other-shipping-methods-when-free-shipping-is-available/ (method_id vs get_method_id).
Thanks !

Anna
3 years ago

Thank you sir!, works brilliant, but if I want to have local pick up enable while they can have the option for the free delivery what do i need to edit?

tarik jari
2 years ago
Reply to  Anna

the same need 🙂

7
0
Would love your thoughts, please comment.x
()
x