Home » WooCommerce: Hide Shipping Rates if Free Shipping Available

WooCommerce: Hide Shipping Rates if Free Shipping Available

by Tutor Aspire

If Free Shipping is available, you possibly don’t want to show the other premium shipping options. WooCommerce shows by default all shipping rates that match a given shipping zone, so it’s not possible to achieve this from the settings alone.

Thankfully, the “woocommerce_package_rates” filter allows us to manipulate the shipping rates before they are returned to the frontend. In this example, we will disable all shipping methods but “Free Shipping” so that free shipping remains the only possible choice.

Here’s the code to add to your functions.php. Enjoy!

Remove a given Flat Rate when Free Shipping is available @ WooCommerce Cart / Checkout

PHP Snippet #1: Unset Specific Shipping Rate When Free Shipping Rate is Available

To find the “shipping rate ID” e.g. “free_shipping:8“, please see the screenshot at the bottom of this other tutorial: https://businessbloomer.com/woocommerce-disable-free-shipping-if-cart-has-shipping-class/
/**
 * @snippet       Hide one shipping rate when Free Shipping is available
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 6
 * @donate $9     https://www.tutoraspire.com
 */
 
add_filter( 'woocommerce_package_rates', 'tutoraspire_unset_shipping_when_free_is_available_in_zone', 9999, 2 );
  
function tutoraspire_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;
}

PHP Snippet #2: Unset ALL Shipping Rates in ALL Zones when ANY Free Shipping Rate is Available

/**
 * @snippet       Hide ALL shipping rates in ALL zones when Free Shipping is available
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 6
 * @donate $9     https://www.tutoraspire.com
 */
 
add_filter( 'woocommerce_package_rates', 'tutoraspire_unset_shipping_when_free_is_available_all_zones', 9999, 2 );
  
function tutoraspire_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;
   } 
}

Shipping rates not hiding after implementing these snippets?

You probably need to:

a) empty the Cart and start testing again

b) clear customer sessions:

Clear Customer Sessions – WooCommerce System Status Tools

You may also like