Skip to content

Instantly share code, notes, and snippets.

@adczk
Created February 5, 2022 18:03
Show Gist options
  • Select an option

  • Save adczk/76e4a793cbfc586d3af8860d744a6487 to your computer and use it in GitHub Desktop.

Select an option

Save adczk/76e4a793cbfc586d3af8860d744a6487 to your computer and use it in GitHub Desktop.
WooCommerce - remove local pickup if free shipping available
<?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
*
* removes only LOCAL PICKUP if FREE SHIPPING is available
* while keeping FREE and other methods in place
* can be modified to remove other shipping options
*
* use as MU plugin
*
* original code here: https://woocommerce.com/document/hide-other-shipping-methods-when-free-shipping-is-available/
* modified by Adam/WPMU DEV
*/
function my_hide_shipping_when_free_is_available( $rates ) {
$new_rates = array();
$is_free = false; // by default let's assume there's no free shipping
// check if there's free shipping available
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$is_free = true; // and set the flag if so
break;
}
}
// if there's free shipping, loop over shipping options again
// and rewrite them removing the "flat_rate" only
if ( $is_free ) {
foreach ( $rates as $rate_id => $rate ) {
if ( 'flat_rate' !== $rate->method_id ) {
$new_rates[ $rate_id ] = $rate;
}
}
}
// return either original shipping (if no free)
// or modified - if there is free
return ! empty( $new_rates ) ? $new_rates : $rates;
}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment