Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Display Only ‘Local Pickup’ if Available in WooCommerce?

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.

How to Display Only 'Local Pickup' if Available in WooCommerce? - Tyche Softwares

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?

How to Display Only 'Local Pickup' if Available in WooCommerce? - Tyche Softwares

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.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x