When Free shipping is available hide other shipping methods except Local pickup in WooCommerce [closed]

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)

  1. This code is already saved on your active theme’s function.php file.
  2. The cart is empty
  3. In a shipping zone settings, disable / save any shipping method, then enable back / save.