In Woocommerce, we use the following code to hide all shipping methods except free shipping:
function my_hide_shipping_when_free_is_available( $rates ) {
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );
Now we would like to keep Express shipping as well as local pickup. Free shipping, however, should be preselected.
Does anyone have an idea how we can customize the code?
Our Shipping methods rate Ids are:
- Normal delivery (Versandkosten):
legacy_flat_rate
- Express delivery (Expressversand):
legacy_flat_rateexpress
- Free shipping (kostenloser Versand):
legacy_free_shipping
- Local Pickup (Abholung vor Ort):
legacy_local_pickup
<input>
fields, but not the tag Id)… – LoicTheAztec