Menu Close

WooCommerce: Hide Shipping If Local Pickup Is Selected

default

Let’s talk about checkout UX: if a user is willing to pick up the item in store, why should there be a shipping form on the checkout?

Well, let’s see how we can hide this dynamically with a bit of PHP and JS!

WooCommerce: disable shipping if local pickup is chosen @ Cart or Checkout

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

/**
 * @snippet       Hide Shipping Fields If Local Pickup
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
  
add_action( 'woocommerce_after_checkout_form', 'bbloomer_disable_shipping_local_pickup' );
  
function bbloomer_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();
			}
		});
	" );  
}
View Source
Posted in WooCommerce Tips