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.

All code below will be placed in our child theme’s functions.php file. If the file is empty the code will go just below the opening <?php tag, otherwise, if it has contents, the code should be placed at the end of the file, before the closing ?> PHP tag if it exists.

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.

add_filter( 'woocommerce_package_rates', 'cssigniter_hide_other_methods_when_free_shipping_is_available', 100 );
function cssigniter_hide_other_methods_when_free_shipping_is_available( $rates ) { $free = array(); foreach ( $rates as $rate_id => $rate ) { if ( 'free_shipping' === $rate->method_id ) { $free[ $rate_id ] = $rate; break; } } return ! empty( $free ) ? $free : $rates;

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.

add_filter( 'woocommerce_package_rates', 'cssigniter_hide_other_methods_except_pickup_when_free_shipping_is_available', 10, 2 );
function cssigniter_hide_other_methods_except_pickup_when_free_shipping_is_available( $rates ) { $my_rates = array(); foreach ( $rates as $rate_id => $rate ) { if ( 'free_shipping' === $rate->method_id ) { $my_rates[ $rate_id ] = $rate; break; } } if ( ! empty( $my_rates ) ) { foreach ( $rates as $rate_id => $rate ) { if ( 'local_pickup' === $rate->method_id ) { $my_rates[ $rate_id ] = $rate; break; } } return $my_rates; } return $rates;

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.

Free shipping and local pickup are available and flat rate shipping is removed.

Wrapping up

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.