Top Navigation Menu

Hide All Other Shipping When Free Shipping Is Available WooCommerce

Code Snippet to Hide All Other Shipping Options When Free Shipping is Available

WooCommerce comes with the ability to offer free shipping out of the box. It is a great feature to entice sales. For example you could offer free shipping for all orders of $75 or more. You determine the amount of the purchase to offer free shipping. However, when a person goes to checkout, the will see ALL of the shipping offers you have.

For example, you offer a flat rate of $9.95 on the order but the order is $80. On the checkout page, the shipping offers will include both the flat rate offer and the free offer, and most likely the flat rate is selected automatically. The customer may not be paying attention and didn’t select the free shipping alternative and ended up getting charged an additional $9.95 for their order. This can cause frustration. I know from experience. This scenario actually happened to me at once. I contacted their support and they refunded me the shipping charge, but it would have been nice to have never been charged for shipping for an order that qualified for free shipping. Yes, it was my fault, because I wasn’t paying attention, but none the less, it should of never happened in the first place.

There is a way to make sure your customers get free shipping when their order qualifies for free shipping. If you use the code snippet below, all other shipping methods will be hidden on orders that qualify for free shipping.

There is a limitation to using this method. If a customer wanted to pay extra for overnight or 2nd day shipping, well those options will be hidden too. So, use this code snippet if you want to only ship orders for free, that qualify for free shipping.

View Our Many WooCommerce Shipping Plugins

Put this code snippet in your Child functions.php

/**     * woocommerce_package_rates is a 2.1+ hook     */    add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );         /**     * Hide shipping rates when free shipping is available     *    * @param array $rates Array of rates found for the package     * @param array $package The package array/object being shipped     * @return array of modified rates     */    function hide_shipping_when_free_is_available( $rates, $package ) {    	    	// Only modify rates if free_shipping is present     	if ( isset( $rates['free_shipping'] ) ) {     	     		// To unset a single rate/method, do the following. This example unsets flat_rate shipping     		unset( $rates['flat_rate'] );     		     		// To unset all methods except for free_shipping, do the following     		$free_shipping          = $rates['free_shipping'];     		$rates                  = array();      		$rates['free_shipping'] = $free_shipping;    	}    	    	return $rates;    }     


No comments yet.

Leave a Reply