The following code will disable other shipping methods excluding “Local Pickup” when “Free shipping” shipping method is available.
Required: Free shipping method need to be set with a minimal order amount.
add_filter( 'woocommerce_package_rates', 'show_hide_shipping_methods', 100 );
function show_hide_shipping_methods( $rates ) {
$free_rate_id = '';
$other_rates_ids = [];
// Loop through available shipping rates
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free_rate_id = $rate_id; // grab "Free shipping" rate ID
}
// Get all other rates Ids (excluding "Free shipping" and "Local pickup" methods)
if ( ! in_array( $rate->method_id, ['free_shipping', 'local_pickup'] ) ) {
$other_rates_ids[] = $rate_id;
}
}
// Disable All other rates Ids when "Free shipping" is available (excl. "Local pickup")
if ( ! empty($free_rate_id) && isset($rates[$free_rate_id]) && sizeof($other_rates_ids) > 0 ) {
foreach ( $other_rates_ids as $rate_id ) {
unset($rates[$rate_id]);
}
}
return $rates;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and work.
Refresh the shipping caches: (required)
- This code is already saved on your active theme’s function.php file.
- The cart is empty
- In a shipping zone settings, disable / save any shipping method, then enable back / save.