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 <meta charset="utf-8">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;
}