How to Hide All Shipping Methods When Free Shipping is Available

Hello friends good morning, hope you are doing good today. Today, I am going to share very useful tips of WooCommerce based on WordPress platform. This tips will help you lot during the development of any WooCommerce websites. In this tutorial, you will hide shipping methods when free shipping available in WooCommerce based online store.

Hide All Shipping Methods When Free Shipping is Available

Sometimes online store owners want to hide all other shipping methods when free shipping is available during the check out process on the website.

Steps to hide shipping methods

Below snippet will hide all shipping methods available in your website if any free shipping available for that order while checkout on the online store. Open your functions.php file and add the below snippets at the end of the file.

<?php

/**
 * woocommerce_package_rates is a 2.1+ hook
 */
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
 
/**
 * Hide shipping rates when free shipping is available
 *
 * @param array $rates Array of rates found for the package
 * @param array $package The package array/object being shipped
 * @return array of modified rates
 */
function hide_shipping_when_free_is_available( $rates, $package ) {
 	
 	// Only modify rates if free_shipping is present
  	if ( isset( $rates['free_shipping'] ) ) {
  	
  		// To unset a single rate/method, do the following. This example unsets flat_rate shipping
  		unset( $rates['flat_rate'] );
  		
  		// To unset all methods except for free_shipping, do the following
  		$free_shipping          = $rates['free_shipping'];
  		$rates                  = array();
  		$rates['free_shipping'] = $free_shipping;
	}
	
	return $rates;
}

?>

If you still need any help, feel free to add your comments in below comment section. I will be happy to help. Do you like & share this article with your friends, and don’t forget to follow us on Facebook and Twitter to learn cool WordPress tutorials.

Leave a Comment

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