Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.