WordPress / Web Development Tutorials
(Best WordPress Tutorials)

CSSHTMLJavaScriptjQueryMySQLPHPSilvaTechnologiesWooCommerceWordpress
Silva Web Designs - Blog

WooCommerce – Hide shipping rates when free shipping is available.

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

 

Nathan da Silva - Profile

Posted by: Nathan da Silva

Nathan is the Founder of Silva Web Designs. He is passionate about web development, website design and basically anything digital-related. His main expertise is with WordPress and various other CMS frameworks. If you need responsive design, SEO, speed optimisation or anything else in the world of digital, you can contact Silva Web Designs here; [email protected]

It’s good to share

Join the discussion

Related Posts

Related - WooCommerce: “You Only Need £££ to Get Free Shipping!” @ Cart

WooCommerce / 14th August 2018

WooCommerce: “You Only Need £££ to Get Free Shipping!” @ Cart

Read More