Customizing the available shipping methods during the checkout process becomes effortless using this handy snippet, allowing you to modify them based on the presence of the ‘local_pickup’ method.
add_filter( 'woocommerce_package_rates', 'ts_unset_shipping_when_local_pickup_is_available_all_zones', 9999, 2 ); function ts_unset_shipping_when_local_pickup_is_available_all_zones( $rates, $package ) { $all_local_pickup_rates = array(); foreach ( $rates as $rate_id => $rate ) { if ( 'local_pickup' === $rate->method_id ) { $all_local_pickup_rates[ $rate_id ] = $rate; break; } } if ( empty( $all_local_pickup_rates ) ) { return $rates; } else { return $all_local_pickup_rates; } }
Output
On the cart page, if ‘local_pickup’ is among the available shipping methods, the code ensures that only ‘local_pickup’ is displayed, hiding all other shipping methods. If ‘local_pickup’ is not available, it leaves the original shipping methods unchanged.
If there is no shipping option such as ‘local-pickup’, then the code will retrieve all other available shipping methods as shown below.
Read Related Article: How to Set a Minimum Order Amount for Free Shipping in WooCommerce?
Similar to the above one, you can hide any shipping methods based on different criteria such as product weight, product quantity, order total, or PO Box address field values. Look into this post that will guide you well on how to hide shipping methods based on certain conditions.