In the world of e-commerce, providing customers with various shipping options is essential for a seamless shopping experience. WooCommerce, a popular e-commerce platform, offers a versatile shipping system that allows you to configure different shipping methods. However, there may be instances when you want to provide a local pickup option and hide shipping options when the customer chooses this method. In this article, we’ll explore how to achieve this in WooCommerce, ensuring a smooth and hassle-free checkout process for your customers.

Why Hide Shipping form for Local Pickup?

Offering a local pickup option can be beneficial for customers who prefer to collect their orders directly from your physical store or designated pickup location. By hiding shipping options when local pickup is selected, you streamline the checkout process, prevent confusion, and avoid unnecessary shipping costs for local customers.

PHP Snippet: Hide Shipping Fields When Local Pickup is Selected in WooCommerce Checkout

Add the below code to the theme functions.php file.

add_action( 'woocommerce_after_checkout_form', 'netzole_disable_shipping_local_pickup' );

function netzole_disable_shipping_local_pickup( $available_gateways ) {

// Part 1: Hide shipping on checkout load
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ( 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
wc_enqueue_js( "
$('#ship-to-different-address input').prop('checked', false).change().closest('h3').hide();
" );
}

// Part 2: Hide shipping on checkout shipping change 
wc_enqueue_js( "
$('form.checkout').on('change','input[name^=\"shipping_method\"]',function() {
var val = $( this ).val();
if (val.match('^local_pickup')) {
$('#ship-to-different-address input').prop('checked', false).change().closest('h3').hide();
} else {
$('#ship-to-different-address input').prop('checked', true).change().closest('h3').show();
}
});
" ); 
}

By following the steps outlined in this article, you’ll enhance the customer experience, eliminate confusion, and provide a seamless checkout process. Implementing this feature ensures that customers who prefer local pickup can complete their transactions efficiently while reducing unnecessary shipping costs.

Related Posts:

Pin It on Pinterest

Shares
Share This