Hide other shipping methods if free shipping is available
Free shipping is a great bonus for any online shopper and greatly helps the customer commit with their purchase. WooCommerce offers free shipping as its available shipping options, but it will continue to display all available shipping methods available to the client regardless if they qualify for free shipping. Since it is highly unlikely that a customer will opt to pay for shipping when they can have it for free, we’ll go ahead and check out a way to hide other shipping methods when a customer qualifies for free shipping.
Install and activate a child theme
The first step on our process here is to create and install a child theme. If you are using one of our themes you can easily grab its child theme from our downloads section. If not, you can read our beginner’s guide on child themes to create your own. This step is essential in order to preserve our changes throughout theme updates.
Hide other shipping methods when free shipping is available
WooCommerce by default will display all shipping methods available to the customer even if it does not make perfect sense sometimes.
Above we can see that customers can choose the flat rate shipping fee while free shipping is available, no customers will prefer to pay for something that they can get for free, so let’s make WooCommerce hide other shipping methods when free shipping is available.
To achieve our goal we will be using the woocommerce_package_rates hook to filter the available shipping rates and keep just the free shipping one.
As seen in the code above, we grab all the available shipping rates, and go through them to see if free shipping appears among them, if it does we grab it and return just that one, otherwise we return them back without processing them at all.
Now only the free shipping is available and preselected for all purchases.
Keep local pickup along with free shipping
While free shipping is very tempting, some customers might prefer to pick up the products themselves to get them in their hands quicker, so let’s give them this option by modifying our code a little bit.
Once again we use the woocommerce_package_rates hook to get the available rates, next we check if free shipping is available, if it is we store it in the $my_rates array and then we check if local pickup is also available, if we find it we store it in the same array and return it providing the customer with just these two shipping options. If free shipping is not available, we return all the available rates without processing them.
With these simple snippets we can make the checkout process more clean for our customers by hiding shipping methods that they are very unlikely to pick. Did you find this tutorial useful? Let us know in the comments below.