Hide WooCommerce paid shipping methods when free shipping is available

20 Views Asked by At

I used the provided code from the WooCommerce website to remove the paid shipping options when free pickup is available and keep the local pickup.

However, it only shows one pickup location and I have multiple pickup locations when free shipping is available as well.

How can I address this?

/**
 * Hide shipping rates when free shipping is available, but keep "Local pickup" 
 * Updated to support WooCommerce 2.6 Shipping Zones
 */
function hide_shipping_when_free_is_available( $rates, $package ) {
    $new_rates = array();

    // Loop through existing rates and retain free shipping if present.
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
            $new_rates[ $rate_id ] = $rate;
            break;
        }
    }

    if ( ! empty( $new_rates ) ) {
        // Save local pickup if it's present.
        foreach ( $rates as $rate_id => $rate ) {
            if ( 'local_pickup' === $rate->method_id ) {
                $new_rates[ $rate_id ] = $rate;
                break;
            }
        }

        return $new_rates;
    }

    return $rates;
}

add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );

0

There are 0 best solutions below