Search the site:

Home Blog How to hide other WooCommerce shipping methods when Free Shipping is available

How to hide other WooCommerce shipping methods when Free Shipping is available

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.

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

Adding a Code Snippet

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

We have used the get_method_id() function which fetches the method id for a shipping method. In our example, when this function will return the value as ‘free_shipping’ it means the shipping method selected in Free Shipping respective of the zone. Method id for other shipping methods are:

Local Pickup: local_pickup
Flat Rate: flat_rate

If you want to hide a specific shipping method for a zone then you need to unset the shipping method by its id for example, ‘flat_rate:3’ where 3 is the unique instance id stored for each shipping method for a zone in the database.

You can find the instance id of the shipping method by right clicking on the shipping method and inspect the element in the developer tool in your browser.

Instance ID

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.

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

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of
from purchased

Subscribe for more offers

To help you in these uncertain times, we are giving the Order Delivery Date plugin at 50% off.
Find out more