Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JeroenSormani/38af8a40b235246a75042f3a481d6b25 to your computer and use it in GitHub Desktop.
Save JeroenSormani/38af8a40b235246a75042f3a481d6b25 to your computer and use it in GitHub Desktop.
New - Hide paid shipping rates when free is available. DOES show Local Pickup. Considered ANY shipping rate that is $0 as free (so the 'Free shipping' shipping method is not a requirement)
<?php
/**
* Copy from here to your (child) themes functions.php
* Recommended to do so via FTP.
*/
/**
* Hide all but the free shipping options when free is available.
*
* Shows ALL free options when available.
* Does NOT take in account local pickup as free shipping - It does show local pickup rates in the list of free shipping rates
* DOES consider non-'free_shipping' rates that have 0 cost as free shipping.
*/
function custom_hide_all_shipping_when_free_is_available( $shipping_rates) {
$free_rates = array();
$free_shipping_available = false;
foreach ( $shipping_rates as $key => $rate ) {
// Check for free rates / don't take in account local pickup
if ( 0 == $rate->cost && $rate->method_id != 'local_pickup' ) {
$free_shipping_available = true;
}
if ( 0 == $rate->cost ) {
$free_rates[ $key ] = $rate;
}
}
// Show all free rates
if ( $free_shipping_available ) {
return $free_rates;
}
return $shipping_rates;
}
add_filter( 'woocommerce_package_rates', 'custom_hide_all_shipping_when_free_is_available' );
@milouser
Copy link

milouser commented Sep 15, 2019

Hi Jeroen

I use the shipping by zone functionality. Unfortunately, it doesn't work for me. When the user has reached free shipping limit, it hide the local pickup option in the checkout cart.

Is this code still actual, or is it also intended to work with shipping by zone? Thanks for your answer.

@JeroenSormani
Copy link
Author

It should still work - the code is show any $0 option (pickup or free ship)

@milouser
Copy link

Thanks! I did some debugging and I see that the function works. Just got overridden.

@Kupodivu
Copy link

It works perfectly. Thanks

@wutzelmutz
Copy link

I was very pleased to find this code doing exactly what I needed - the only thing is that it sets local pickup as default and lists it first although I have set it to the end of the list in the backend. Is there any way to mark the free shipping as default instead of local pickup and move pickup to the end of the list?

@ChristianPorsche
Copy link

Hi,

It works for me not completely. It works when it is free shipping, but ones it isn't free shipping, but local pickup is enabled, the flat rate isn't displayed...so only the local pickup is displayed. It looks like the script thinks local pickup is free, so that that's the shipping a customer should choose. But a flat rate is also possible of course.

@iqtasos1
Copy link

iqtasos1 commented Jun 1, 2022

Realy handy thanks for your contribution !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment