If you want to set “Local pickup” as the default shipping method on the cart and checkout page, use the below snippet.
add_action( 'woocommerce_before_cart', 'ts_default_shipping_method', 5 ); function ts_default_shipping_method(){ if( count( WC()->session->get('shipping_for_package_0')['rates'] ) > 0 ){ foreach( WC()->session->get('shipping_for_package_0')['rates'] as $rate_id =>$rate) if($rate->method_id == 'local_pickup'){ $default_rate_id = array( $rate_id ); break; } WC()->session->set('chosen_shipping_methods', $default_rate_id ); } }
Output
When a customer adds an item to their cart and clicks on “View Cart,” they are redirected to the cart page where the default shipping method is set to “Local Pickup” as shown below.
Instead of making any shipping method as the default one, you can also hide the shipping method which you don’t require. This post will help you to hide WooCommerce shipping methods for certain conditions based on cart total, product weight, product quantity, etc.