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.
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.
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.
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.
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:
- 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”.
- 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.
- 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.
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. 🙂