How to Hide Woocommerce Checkout Fields When Local Pickup is Selected?

Most annoying thing with the Woocommerce Local Pickup shipping method is that even though no shipping information is needed, you still need to fill in all the address and postcode fields. But what if you could automatically hide those fields if the local pickup method is chosen? Well, take a look at this video below, and then you can.

Video: How to Hide Woocommerce Checkout Fields When Local Pickup is Selected?

Code snippet used in the video

/* This piece of code will hide fields for the chosen method.
.hide_pickup {
    display: none !important;
}
*/
 
// Hide Local Pickup shipping method
add_filter( 'woocommerce_checkout_fields', 'hide_local_pickup_method' );
function hide_local_pickup_method( $fields_pickup ) {
    // change below for the method
    $shipping_method_pickup ='local_pickup:4';
    // change below for the list of fields. Add (or delete) the field name you want (or don’t want) to use
    $hide_fields_pickup = array( 'billing_company', 'billing_country', 'billing_postcode', 'billing_address_1', 'billing_address_2' , 'billing_city', 'billing_state');
 
    $chosen_methods_pickup = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping_pickup = $chosen_methods_pickup[0];
 
    foreach($hide_fields_pickup as $field_pickup ) {
        if ($chosen_shipping_pickup == $shipping_method_pickup) {
            $fields_pickup['billing'][$field_pickup]['required'] = false;
            $fields_pickup['billing'][$field_pickup]['class'][] = 'hide_pickup';
        }
        $fields_pickup['billing'][$field_pickup]['class'][] = 'billing-dynamic_pickup';
    }
    return $fields_pickup;
}
// Local Pickup - hide fields
add_action( 'wp_head', 'local_pickup_fields', 999 );
function local_pickup_fields() {
    if (is_checkout()) :
    ?>
    <style>
        .hide_pickup {display: none!important;}
    </style>
    <script>
        jQuery( function( $ ) {
            if ( typeof woocommerce_params === 'undefined' ) {
                return false;
            }
            $(document).on( 'change', '#shipping_method input[type="radio"]', function() {
                // change local_pickup:4 accordingly
            $('.billing-dynamic_pickup').toggleClass('hide_pickup', this.value == 'local_pickup:4');
            });
        });
    </script>
    <?php
    endif;
}

How to hide Woocommerce shipping address section if Local pickup is selected?

Well, it seems like a reasonable wish to hide entire Woocommerce shipping address section if local pickup is selected. That means the “Ship to different address” select box will be hidden if you use this snippet here below.

add_action( 'woocommerce_after_checkout_form', 'hide_ship_to_section' );
  
function hide_ship_to_section( $available_gateways ) {
    
   $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
   $chosen_shipping = $chosen_methods[0];
   if ( 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
   ?>
      <script type="text/javascript">
         jQuery('#customer_details .col-2').fadeOut();
      </script>
   <?php  
   } 
   ?>
      <script type="text/javascript">
         jQuery('form.checkout').on('change','input[name^="shipping_method"]',function() {
            var val = jQuery( this ).val();
            if (val.match("^local_pickup")) {
                     jQuery('#customer_details .col-2').fadeOut();
               } else {
               jQuery('#customer_details .col-2').fadeIn();
            }
         });
      </script>
   <?php
  
}

How to Hide Woocommerce Checkout Fields Based on Shipping Methods?

Now, if you would like to hide Woocommerce checkout fields for multiple chosen shipping methods, then it’s a bit trickier. There are couple of things I need to point out for this code here below.

So, this snippet here below does this:

  • If Local pickup (local_pickup:2) is selected, Billing country, postcode, address 1, address 2 and city fields are disabled.
  • If Local Pickup 2 (local_pickup:8) is selected, Billing country, postcode, address 1, address 2 and city fields are disabled.
  • If Local pickup (multiparcels_smartpost_pickup_point:7) is selected, Billing country, postcode, address 1, address 2 and city and phone fields are disabled.

If you would like to use this snippet, then take a look at line 30 for changing your shipping ID. Line 33 contains fields to be hidden. And line 70 should once again contains your shipping method. So, everything between lines 25 and 76 contains code for Local pickup method. Lines 78-130 is used for Local pickup 2 and lines 132-182 are for SmartPost shipping method.

In a similar way you can add (or remove) your own additional shipping methods.

/* CSS classes for hiding fields
.hide_local {
    display: none !important;
}
.hide_local2 {
    display: none !important;
}
.hide_smartpost {
    display: none !important;
}

AVAILABLE BILLING FIELDS
billing_company = COMPANY
billing_country = COUNTRY
billing_postcode = POSTCODE
billing_address_1 = ADDRESS 1
billing_address_2 = ADDRESS 2
billing_city = CITY
billing_last_name = LAST NAME
billing_first_name = FIRST NAME
billing_phone = PHONE
*/


// Hide local pickup fields
add_filter( 'woocommerce_checkout_fields', 'wpsh_hide_local_pickup_fields' );
function wpsh_hide_local_pickup_fields( $fields_itella ) {
    
  // Add your own shipping method ID
    $shipping_method_itella ='local_pickup:2'; 
  
    // change below for the list of fields
    $hide_fields_itella = array( 'billing_company', 'billing_country', 'billing_postcode', 'billing_address_1', 'billing_address_2' , 'billing_city' );

    $chosen_methods_itella = WC()->session->get( 'chosen_shipping_methods' );
    // uncomment below line and reload checkout page to check current $chosen_methods
    // print_r($chosen_methods);
    $chosen_shipping_itella = $chosen_methods_itella[0];

    foreach($hide_fields_itella as $field_itella ) {
        if ($chosen_shipping_itella == $shipping_method_itella) {
            $fields_itella['billing'][$field_itella]['required'] = false;
            $fields_itella['billing'][$field_itella]['class'][] = 'hide_local';
        }
        $fields_itella['billing'][$field_itella]['class'][] = 'billing-dynamic_itella';
    }
    return $fields_itella;
}

// Local pickup Ajax

add_action( 'wp_head', 'wpsh_local_pickup_ajax', 999 );
function wpsh_local_pickup_ajax() {
    if (is_checkout()) :
    ?>
    <style>
        .hide_local {display: none!important;}
    </style>
    <script>
        jQuery( function( $ ) {

            // woocommerce_params is required to continue, ensure the object exists
            if ( typeof woocommerce_params === 'undefined' ) {
                return false;
            }

            $(document).on( 'change', '#shipping_method input[type="radio"]', function() {
			  
               // Add your own shipping method ID
            $('.billing-dynamic_itella').toggleClass('hide_local', this.value == 'local_pickup:2');  
            });
        });
    </script>
    <?php
    endif;
}

// // Hide local pickup 2 fields

add_filter( 'woocommerce_checkout_fields', 'wpsh_hide_local_pickup2_fields' );
function wpsh_hide_local_pickup2_fields( $fields_omniva ) {
  
// Add your own shipping method ID
    $shipping_method_omniva ='local_pickup:8'; 
  
    // change below for the list of fields
    $hide_fields_omniva = array( 'billing_company', 'billing_country', 'billing_postcode', 'billing_address_1', 'billing_address_2' , 'billing_city' );

    $chosen_methods_omniva = WC()->session->get( 'chosen_shipping_methods' );
    // uncomment below line and reload checkout page to check current $chosen_methods
    // print_r($chosen_methods);
    $chosen_shipping_omniva = $chosen_methods_omniva[0];

    foreach($hide_fields_omniva as $field_omniva ) {
        if ($chosen_shipping_omniva == $shipping_method_omniva) {
            $fields_omniva['billing'][$field_omniva]['required'] = false;
            $fields_omniva['billing'][$field_omniva]['class'][] = 'hide_local';
        }
        $fields_omniva['billing'][$field_omniva]['class'][] = 'billing-dynamic_omniva';
    }

    return $fields_omniva;
}

// Local pickup 2 Ajax
add_action( 'wp_head', 'wpsh_hide_local_pickup2_ajax', 999 );
function wpsh_hide_local_pickup2_ajax() {
    if (is_checkout()) :
    ?>
    <style>
        .hide_local2 {display: none!important;}
    </style>
    <script>
        jQuery( function( $ ) {

            // woocommerce_params is required to continue, ensure the object exists
            if ( typeof woocommerce_params === 'undefined' ) {
                return false;
            }

            $(document).on( 'change', '#shipping_method input[type="radio"]', function() {
			  
                // Add your own shipping method ID
            $('.billing-dynamic_omniva').toggleClass('hide_local2', this.value == 'local_pickup:8');   
            });
        });
    </script>
    <?php
    endif;
}

// Hide SmartPost fields
add_filter( 'woocommerce_checkout_fields', 'wpsh_hide_smartpost_fields' );
function wpsh_hide_smartpost_fields( $fields_pickup ) {
  
   // Add your own shipping method ID
    $shipping_method_pickup ='multiparcels_smartpost_pickup_point:7'; 
  
    // change below for the list of fields
    $hide_fields_pickup = array( 'billing_company', 'billing_country', 'billing_postcode', 'billing_address_1', 'billing_address_2' , 'billing_city', 'billing_phone' );

    $chosen_methods_pickup = WC()->session->get( 'chosen_shipping_methods' );
    // uncomment below line and reload checkout page to check current $chosen_methods
    // print_r($chosen_methods);
    $chosen_shipping_pickup = $chosen_methods_pickup[0];

    foreach($hide_fields_pickup as $field_pickup ) {
        if ($chosen_shipping_pickup == $shipping_method_pickup) {
            $fields_pickup['billing'][$field_pickup]['required'] = false;
            $fields_pickup['billing'][$field_pickup]['class'][] = 'hide_smartpost';
        }
        $fields_pickup['billing'][$field_pickup]['class'][] = 'billing-dynamic_pickup';
    }

    return $fields_pickup;
}
// Samrtpost Ajax
add_action( 'wp_head', 'wpsh_hide_smartpost_fields_ajax', 999 );
function wpsh_hide_smartpost_fields_ajax() {
    if (is_checkout()) :
    ?>
    <style>
        .hide_smartpost {display: none!important;}
    </style>
    <script>
        jQuery( function( $ ) {

            // woocommerce_params is required to continue, ensure the object exists
            if ( typeof woocommerce_params === 'undefined' ) {
                return false;
            }

            $(document).on( 'change', '#shipping_method input[type="radio"]', function() {
			  
               // Add your own shipping method ID
            $('.billing-dynamic_pickup').toggleClass('hide_smartpost', this.value == 'multiparcels_smartpost_pickup_point:7'); 
            });
        });
    </script>
    <?php
    endif;
}

Useful Tips

Here are some of my favorite Wordpress tools

Thank you for reading this article. I hope you found it helpful as you build your own websites and e-commerce sites. Here are some tools I use as a Wordpress developer and enthusiast that I hope you’ll also find helpful.

These are affiliate links, so if you do decide to use any of them, I’ll earn a commission and this helps me create these tutorials and make Youtube videos. But in all honesty, these are the exact tools that I use and recommend to everyone, even my friends and family.

Themes: For the last couple of years I have two go-to themes which I use for every kind of projects. Those two themes are Blocksy theme and Kadence Theme. On this site and my Youtube channel you’ll see a lot of tutorials I have made about them. If you would like to get a 10% discount for both of them then:

Code Snippets manager: WPCodeBox allows you to add code snippets to your site. Also, it allows you to build and manage your WordPress Code Snippets library in the cloud. You can grab it with the 20% discount here (SAVE 20% Coupon: WPSH20).

Contact forms: There are hundreds of contact forms out there but Fluent Forms is the one I like the most. If you need a 20% discount then use this link (save 20% coupon is WPSH20).

Gutenberg add-ons: If I need a good Gutenberg blocks add-on then Kadence Blocks is the one I have used the most. You’ll get 10% discount with the coupon SIMPLEHACKS here.

Website migration: While building a website you probably need a good plugin that can help you with the migration, backups, restoration and staging sites. Well, WpVivid is the one I have used for the last couple of years. If you use this link along with the WPSH20 coupon you’ll get 20% discount.

Woocommerce extensions: There are a bunch of Woocommerce extensions that I really like but the one that stands really out is Advanced Dynamic Pricing. Once again, you’ll get a 20% discount if you use this link here (save 20% coupon is WPSH20)

Web Hosting: If you would like to have a really fast and easy to use managed cloud hosting, then I recommend Verpex Hosting (see my review here). Btw, this site is hosted in Verpex.)

To see all my of most up-to-date recommendations, check out this resource that I made for you!

Do you want to thank me and buy me a beer?

Every donation is entirely welcome but NEVER required. Enjoy my work for free but if you would like to thank me and buy me a beer or two then you can use this form here below.

Choose amount

Best selling plugins

Janek T.
Janek T.

I am a Wordpress enthusiast who has been making websites since 2011. In this site I am offering simple to follow tips on how to use Wordpress and Woocommerce.
If you want to be the first to be notified about the new tutorials then please subscribe to my Youtube channel here
Follow me in Twitter here

Articles: 89