How to Hide Shipping Rates if Free Shipping Available WooCommerce

WooCommerce Hide Shipping Rates if Free Shipping AvailableWooCommerce stores are very easy to manage and moderate, whether you are good on the technical side or not. Moreover, it has a user-friendly back end that has a plethora of options to tweak your store.

It is worth mentioning that shipping is an inseparable operation if you want to have a successful online store. With increasing locations and setups for the shipping processes, you keep adding shipping methods to your store. For local customers residing near your physical stores and warehouses, you can introduce free shipping or local pickup facilities.

However, one thing is clear here. The increasing shipping method and their availability on the cart page, even when not required, could be adversely affecting your site’s user experience or confusing the buyers. This, in turn, reduces the sales in your WooCommerce store.

For example, if Free Shipping is available, you may not want to show the other paid shipping options. WooCommerce, by default, displays all the shipping rates that match a given shipping zone, so it is not possible to achieve this from the settings alone. You need PHP for that.WooCommerce shipping zone

In this tutorial, I will show you how to disable all shipping methods but “Free Shipping” so that free shipping remains the only possible choice.cart with multiple shipping options

Steps to Hide Shipping Rates if Free Shipping is Available

Here are the steps that you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file to add the function to unset specific shipping rate when free shipping rate is available.
  3. Add the following code to the functions.php file:
/**

 *           Hide one shipping option in one zone when Free Shipping is available

*/

add_filter( 'woocommerce_package_rates', 'njengah_unset_shipping_when_free_is_available_in_zone', 10, 2 );

  function njengah_unset_shipping_when_free_is_available_in_zone( $rates, $package ) {

     // Only unset rates if free_shipping is available

if ( isset( $rates['free_shipping:8'] ) ) {

     unset( $rates['flat_rate:1'] );

}    

  return $rates;

  }
  1. The next step is to unset all shipping rates in all zones when any free shipping rate is available. This can be achieved by adding the following code to the functions.php file:
/**

*       Hide ALL shipping rates in ALL zones when Free Shipping is available

*/

add_filter( 'woocommerce_package_rates', 'njengah_unset_shipping_when_free_is_available_all_zones', 10, 2 );

function njengah_unset_shipping_when_free_is_available_all_zones( $rates, $package ) {

$all_free_rates = array();

foreach ( $rates as $rate_id => $rate ) {

if ( 'free_shipping' === $rate->method_id ) {

$all_free_rates[ $rate_id ] = $rate;

break;

}

}

if ( empty( $all_free_rates )) {

return $rates;

} else {

return $all_free_rates;

}

}
  1. Once that is done, go back to the cart page and refresh it. You should see this:free shipping

Conclusion

This post shows how you can hide the shipping rates if the Free Shipping method is available. The first thing you need to do is to unset the specific shipping rate when a free shipping rate is available. The next step is to unset all shipping rates in all zones when any free shipping rate is available. If the shipping rates are not working after implementing the PHP code snippets, empty the cart and start testing again.

Similar Articles