Created
February 26, 2025 09:28
-
-
Save cristacheda/7719104baf11d4d40f3cb25a478806cb to your computer and use it in GitHub Desktop.
WooCommerce hide shipping rates when free shipping is available, but allow local pickup if the order qualifies for free shipping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Hide shipping rates when free shipping is available, but allow local pickup if the order qualifies for free shipping. | |
* | |
* @param array $rates Array of rates found for the package. | |
* @return array | |
*/ | |
function woo_hide_shipping_when_free_is_available( $rates ) { | |
$free = array(); | |
$allowed_methods = array(); // Array to store allowed methods (free shipping + local pickup) | |
foreach ( $rates as $rate_id => $rate ) { | |
if ( 'free_shipping' === $rate->method_id ) { | |
$free[ $rate_id ] = $rate; | |
} | |
} | |
// If free shipping is available, allow it and also keep local pickup | |
if ( ! empty( $free ) ) { | |
foreach ( $rates as $rate_id => $rate ) { | |
if ( 'local_pickup' === $rate->method_id ) { | |
$allowed_methods[ $rate_id ] = $rate; | |
} | |
} | |
return array_merge( $free, $allowed_methods ); | |
} | |
return $rates; // If free shipping is not available, return all shipping methods | |
} | |
add_filter( 'woocommerce_package_rates', 'woo_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