Tutorial, WooCommerce

Hide Other Shipping Methods when Free shipping available on woocommerce checkout to Maximizing Customer Satisfaction

If you’re running a WooCommerce store, you may want to offer free shipping on certain products or when a customer reaches a certain cart total. However, it can be confusing for customers if they see other shipping options alongside the free shipping option. In this blog post, we’ll show you how to hide other shipping methods when free shipping is available on WooCommerce using a simple code snippet.

To get started, you’ll need to add the following code snippet to your functions.php file:


/**
* Hide shipping rates when free shipping is available.
* Updated to support WooCommerce 2.6 Shipping Zones.
*
* @param array $rates Array of rates found for the package.
* @return array
*/
function my_hide_shipping_when_free_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;
}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );

This code creates a function called my_hide_shipping_when_free_is_available which takes an array of shipping rates as its parameter. The function then loops through the shipping rates and checks if there is a free shipping option available. If there is, it creates a new array with only the free shipping option and returns it. If there isn’t, it returns the original array of shipping rates.

The final line of the code uses the add_filter function to apply this function to the woocommerce_package_rates filter with a priority of 100. This means that the function will be executed after other shipping methods have been loaded.

Once you’ve added this code to your functions.php file, you should see that when a customer is eligible for free shipping, the other shipping methods will no longer be displayed on the checkout page.

In conclusion, hiding other shipping methods when free shipping is available can make the checkout process smoother and less confusing for customers. With the code snippet we’ve provided, you can easily achieve this on your WooCommerce store.

Leave a Reply

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