Terry Tsang

Logo - Terry Tsang
About
WooCommerce Show “Free Shipping” Only When It’s Available

WooCommerce Show “Free Shipping” Only When It’s Available

WooCommerce will show all shipping methods that match the customer and the cart contents as the default setting. This means “Free Shipping” will also show along with “Flat Rate” and other shipping methods.

What if you would like to hide other shipping methods and show only “Free Shipping” when it’s available?

Here is the code you may copy and paste at the bottom of functions.php where you can access it through > Theme File Editor > functions.php.

function tt_hide_shipping_when_free_is_available( $rates ) {
	$method = array();
	foreach ( $rates as $rate_id => $rate ) {
		if ( $rate->method_id === 'free_shipping' ) {
		    $methods[ $rate_id ] = $rate;
	            break;
		}
	}
	return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'tt_hide_shipping_when_free_is_available', 100 );

Leave a Comment