Menu Close

WooCommerce – Hide shipping rates when free shipping is available.

default

Have you noticed with WooCommerce that when free shipping is available it displays the free shipping as a shipping method but still charges the flat rate? Annoying right?

If free shipping is available then it should only show the free shipping method in my opinion. They’re may be exceptions when you want to display other shipping methods for a quicker delivery service but for the particular site I built there is just ‘flat rate’ and ‘free shipping’ methods available.

So, how can this be done? Simply add the following code in your functions.php file and this will hide any other shipping methods if free shipping is available to the customer:-


<?php
/**
 * 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 );
View Source
Posted in WordPress