1. Home
  2. »
  3. Woocommerce
  4. »
  5. Hide other shipping methods when “Free Shipping” is available

Hide other shipping methods when “Free Shipping” is available

How do I show only Free Shipping?

Hides everything but free_shipping if it’s available and is compatible with Shipping Zones.


/**
 * 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 );

How do I show only Local Pickup and Free Shipping?

Hides everything but free_shipping and local_pickup if it’s available and is compatible with Shipping Zones.

/**
 * Hide shipping rates when free shipping is available, but keep "Local pickup" 
 * Updated to support WooCommerce 2.6 Shipping Zones
 */

function hide_shipping_when_free_is_available( $rates, $package ) {
	$new_rates = array();
	foreach ( $rates as $rate_id => $rate ) {
		// Only modify rates if free_shipping is present.
		if ( 'free_shipping' === $rate->method_id ) {
			$new_rates[ $rate_id ] = $rate;
			break;
		}
	}

	if ( ! empty( $new_rates ) ) {
		//Save local pickup if it's present.
		foreach ( $rates as $rate_id => $rate ) {
			if ('local_pickup' === $rate->method_id ) {
				$new_rates[ $rate_id ] = $rate;
				break;
			}
		}
		return $new_rates;
	}

	return $rates;
}

add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );

Show only free shipping in all states except

Show only free shipping in all states except the exclusion list. Hide free shipping if the customer is in one of the states listed:

/**
 * Hide ALL shipping options when free shipping is available and customer is NOT in certain states
 *
 * Change $excluded_states = array( 'AK','HI','GU','PR' ); to include all the states that DO NOT have free shipping
 */
add_filter( 'woocommerce_package_rates', 'hide_all_shipping_when_free_is_available' , 10, 2 );

/**
 * Hide ALL Shipping option when free shipping is available
 *
 * @param array $available_methods
 */
function hide_all_shipping_when_free_is_available( $rates, $package ) {
 
	$excluded_states = array( 'AK','HI','GU','PR' );
	if( isset( $rates['free_shipping'] ) AND !in_array( WC()->customer->shipping_state, $excluded_states ) ) :
		// Get Free Shipping array into a new array
		$freeshipping = array();
		$freeshipping = $rates['free_shipping'];
 
		// Empty the $available_methods array
		unset( $rates );
 
		// Add Free Shipping back into $avaialble_methods
		$rates = array();
		$rates[] = $freeshipping;
 
	endif;
 
	if( isset( $rates['free_shipping'] ) AND in_array( WC()->customer->shipping_state, $excluded_states ) ) {
 
		// remove free shipping option
		unset( $rates['free_shipping'] );
 
	}

	return $rates;
}

Leave a Comment

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

অভিনন্দন !

আপনার আবেদনটি প্রাথমিকভাবে গ্রহণ করা হলো। আপনার পেমেন্ট ভেরিফাই করে আপনার ভর্তি কন্ফার্ম করা হবে। ২৪ ঘন্টার মধ্যে আপনাকে আপনার ব্যাচ এর ফেইসবুক এবং ম্যাসেঞ্জারের গ্রুপ লিংক সহ কনফার্মেশন মেইল পাঠানো হবে আপনার মেইলে।

Congratulations !

আপনার Registration সফল হয়েছে। ক্লাস শুরু হবার ১ ঘণ্টা পূর্বে আপনার মেইল এ ক্লাস এর Zoom Link পাঠিয়ে দেয়া হবে।

ধন্যবাদ

Scroll to Top

Let's Talk With Me

Give us a call or fill in the form below and we’ll contact you. We endeavor to answer all inquiries within 24/7 hours on business days.