Skip Address Fields When Local Pickup Is Chosen
Hides and stops requiring the street address fields on the classic checkout when the customer picks Local Pickup, since you will not deliver the order.
Opens WPIDE on
Code
$wpide_pickup_hidden = array( 'address_1', 'address_2', 'city', 'postcode', 'state', 'company' ); // Billing fields to skip.
$wpide_pickup_selected = function () {
$chosen = WC()->session ? (array) WC()->session->get( 'chosen_shipping_methods', array() ) : array();
// phpcs:ignore WordPress.Security.NonceVerification -- WooCommerce verifies the checkout nonce.
if ( isset( $_POST['shipping_method'] ) ) {
$chosen = array_map( 'wc_clean', (array) wp_unslash( $_POST['shipping_method'] ) );
}
foreach ( $chosen as $method ) {
if ( 0 === strpos( (string) $method, 'local_pickup' ) ) {
return true;
}
}
return false;
};
add_filter( 'woocommerce_checkout_fields', function ( $fields ) use ( $wpide_pickup_hidden, $wpide_pickup_selected ) {
if ( ! $wpide_pickup_selected() ) {
return $fields;
}
foreach ( $wpide_pickup_hidden as $key ) {
if ( isset( $fields['billing'][ 'billing_' . $key ] ) ) {
$fields['billing'][ 'billing_' . $key ]['required'] = false;
}
}
return $fields;
}, 30 );
add_action( 'wp_footer', function () use ( $wpide_pickup_hidden ) {
if ( ! is_checkout() || is_wc_endpoint_url() ) {
return;
}
$selectors = implode( ',', array_map( function ( $key ) {
return '#billing_' . $key . '_field';
}, $wpide_pickup_hidden ) );
?>
<script>
jQuery( function ( $ ) {
var fields = <?php echo wp_json_encode( $selectors ); ?>;
function toggle() {
var method = $( 'input[name^="shipping_method"]:checked, select[name^="shipping_method"], input[type="hidden"][name^="shipping_method"]' ).first().val() || '';
$( fields ).toggle( method.indexOf( 'local_pickup' ) !== 0 );
}
$( document.body ).on( 'updated_checkout', toggle );
toggle();
} );
</script>
<?php
} );
Notes
Name, email and phone stay required, and the address fields come back if the customer switches to a delivery method.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.