Hide other shipping methods if free shipping is available

Posted On
Hide other shipping methods if free shipping is available WordPress template

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.

4 responses to “Hide other shipping methods if free shipping is available”

  1. Hi, Nik, thanks for the guide because from my research, lots of customers love the word “FREE”, and when it comes to Free Shopping, it is one of the best marketing strategies to win the hearts of customers.

    Going forward, there are website owners who do not like technical stuff like pasting codes with the fear of messing things up.

    So, is there a package on CSSigniter for site wonders to pay a little fee for the set-up process and other add-ons?

    • Nik says:

      Hello.

      You could provide this feature to your clients as an installable plugin so they won’t have to paste any code. You can read this small guide on how to create a simple WordPress plugin, copy the code inside and distribute it to your customers.

  2. Alex says:

    Hi,

    Can the local pickup code be revised to show multiple pick up locations?
    The current code only shows the first pickup location.

    Kind Regards

    • Nik says:

      Hello Alex.
      It most likely can since there is a WooCommerce plugin called Local Pickups Plus which seems to offer such functionality. However implementing this is quite complex and far outside the scope of this post.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get access to all WordPress themes & plugins

24/7 Support Included. Join 115,000+ satisfied customers.

Pricing & Sign Up

30-day money-back guarantee. Not satisfied? Your money back, no questions asked.

Back to top