Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hide all/some shipping options when free shipping is available
/**
* 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;
}
@iyfm

This comment has been minimized.

Copy link

commented May 6, 2014

Hi,

Trying to copy this code directly to our functions.php template file. But we get errors. Did I miss something?

Can someone maybe have a look at our functions.php?

Regards

@simonlk

This comment has been minimized.

Copy link

commented May 27, 2014

Make sure you read the instructions in the code iyfm! I commented out line 19 so that all other shipping methods would be hidden and it works great.

@ghost

This comment has been minimized.

Copy link

commented Aug 16, 2014

Hello Renegadesk,
Please help me!
I would like to hide the other shipping options, when the free shipping option is available.
Please write to me excatly, where and what code have to paste?
Thanks in advance,

@alemarengo

This comment has been minimized.

Copy link

commented Mar 29, 2015

Are you sure guys that this snippet will work again? I added to my function.php, but I have local_pickup, free_shipping and flat_rate that should be unset but it's not. I created a flat_rate just to make sure to have one, but I want to hide it. 99,99% of sells on my ecommerce have free_shipping, 0,01% have local_pickup. Flat_rate shouldn't be there, it's annoying for my customers. How can I fix it?

@ghost

This comment has been minimized.

Copy link

commented Jul 21, 2015

The example does not seem to work!

@ghost

This comment has been minimized.

Copy link

commented Jul 21, 2015

The example does not seem to work! Okay I had a caching problem with WooCommerce Transients

@ikrmedia

This comment has been minimized.

Copy link

commented Jan 29, 2016

Works great! THANKS!

@Flosisseus

This comment has been minimized.

Copy link

commented Jun 16, 2016

Worked great until the update to 2.6.
Did they changed the hooks with the new "shipping zones"-feature?

@cihankaba

This comment has been minimized.

Copy link

commented Feb 16, 2017

I'm just here to thank you. Graet job, thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.