WooCommerce PHP Beginner Requires WooCommerce

Hide Other Shipping Rates When Free Shipping Is Available

When a customer qualifies for free shipping, show only the free option instead of making them pick it from a list of paid ones.

Tested on WordPress 7.1 PHP 8.3 Updated
functions.php 8 lines
add_filter( 'woocommerce_package_rates', function ( array $rates ): array {
	$free = array_filter( $rates, fn ( $rate ) => 'free_shipping' === $rate->get_method_id() );

	// Keep local pickup available alongside free shipping.
	$pickup = array_filter( $rates, fn ( $rate ) => in_array( $rate->get_method_id(), [ 'local_pickup', 'pickup_location' ], true ) );

	return $free ? $free + $pickup : $rates;
}, 100 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

When an order qualifies for free shipping, WooCommerce still lists your paid rates next to it. Customers can pay for shipping by mistake. This snippet removes the paid options whenever free shipping is available.

How it works

WooCommerce calculates the available rates for each shipping package and passes them through woocommerce_package_rates. The snippet looks for rates from the Free shipping method. If there are any, it returns only those, plus local pickup so customers can still collect in person.

It runs at priority 100, after most shipping plugins have added their rates.

Things to watch

  • WooCommerce caches shipping rates in the customer’s session. After adding the snippet, change the cart or the address to see the update.
  • If you want to keep an express option next to free shipping, add its method id to the $pickup check.
  • This works with both the classic and block-based Cart and Checkout.

How to test it

Add enough to the cart to qualify for free shipping and open the cart. Only Free shipping (and local pickup, if you offer it) should be listed.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only