Hide other shipping rates when free shipping is available in Woocommerce WordPress

Hide other shipping rates when free shipping is available in Woocommerce WordPress


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