How to Hide Shipping Rates if Free Shipping Available in WooCommerce?

Sometimes, you tend to offer cost-free shipping to your customers. In such cases, you do not want to display paid shipping options for obvious reasons. However, you cannot manually unset paid shipping options through WooCommerce settings. Instead, you need to code it. In this article, you will learn how to hide shipping rates if free shipping available. 

You can enable this function with simple PHP. Enjoy reading this simple & short guide. 

Image suggesting to hide shipping rates if free shipping available

PHP Snippet 1: Hide shipping rate if free shipping available – Specific Shipping Rate

add_filter( 'woocommerce_package_rates', 'phpsof_unset_shipping_when_free_is_available_in_zone', 10, 2 );
   
function phpsof_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: Hide Shipping Rates if Free Shipping Available – All Shipping Rates

add_filter( 'woocommerce_package_rates', 'phpsof_unset_shipping_when_free_is_available_all_zones', 10, 2 );
   
function phpsof_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;
} 
 
}

Unable to unset paid shipping rates after applying this PHP snippet?

Follow these steps to make PHP snippet work:

  1. Empty the cart to test again
  2. Click on ‘clear customer sessions’ in WooCommerce settings

Image suggesting to clear customer sessions to remove paid shipping rates

Where do you add this snippet?

So, this is how you can hide shipping rates if free shipping available. Also, it is easier to activate this function with this code. So, to apply this code, just add this PHP snippet at your child theme function.php file’s bottom. It should be placed before “?>”, if you have it there. Apart from this, CSS goes in your child theme style.css file. Make sure that you are editing these files in a right manner for best results.

Is this snippet still valid?

So, this is how you can learn about hide shipping rates when you provide product delivery at no cost to customers. I have applied this code on the Storefront theme and WordPress friendly hosting PHP 7.3. Let me know if everything works as expected when you code. Share it further if you find this snippet useful for you and it had saved your time. 

Also Read, How to Send Checkout Details to Email Recipients in WooCommerce?

How to Display Shipping Message after Country Selection at Checkout?

Important links: WooCommerce – https://woocommerce.com/