Hide Other Shipping Rates When Free Shipping Is Available
When the cart qualifies for free shipping, shows only the free shipping and local pickup options instead of all rates.
Opens WPIDE on
Code
add_filter( 'woocommerce_package_rates', function ( $rates ) {
$kept = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->get_method_id() ) {
$kept[ $rate_id ] = $rate;
}
}
if ( ! $kept ) {
return $rates;
}
foreach ( $rates as $rate_id => $rate ) {
if ( in_array( $rate->get_method_id(), array( 'local_pickup', 'pickup_location' ), true ) ) {
$kept[ $rate_id ] = $rate;
}
}
return $kept;
}, 100 );
Notes
Clear the shipping cache (WooCommerce > Status > Tools) or update the cart to see the change.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.