WooCommerce: Hide other shipping methods when “Free Shipping” is available except for “Local Pickup”

With the snippet found on https://docs.woocommerce.com/document/hide-other-shipping-methods-when-free-shipping-is-available/, you can hide other shipping methods when “Free Shipping” is available. The problem with this snippet is that is will also hide the “Local Pickup” shipping method.

If you want to keep the “Local Pickup” shipping method, try this snippet instead:

/**
 * 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_shipping = false;
	$free_shipping_rates = array();
	
	foreach ( $rates as $rate_id => $rate ) {
		if ( 'free_shipping' === $rate->method_id ) {
			$free_shipping_rates[ $rate_id ] = $rate;
			$free_shipping = true;
		}
		elseif ( 'local_pickup' === $rate->method_id ) {
			$free_shipping_rates[ $rate_id ] = $rate;
		}
	}

	if( $free_shipping ) {
		return $free_shipping_rates;
	}

	return $rates;

}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );

Leave a Reply

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

Overtuigd?

Neem dan contact op voor een offerte of vrijblijvend gesprek.
Stuur een e-mail naar [email protected] of maak gebruik van het contactformulier.