Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Validate Shipping ZIP Code During Checkout in WooCommerce?

To ensure accurate deliveries, you want to restrict customers from entering certain ZIP codes during checkout. This code ensures that customers can only proceed with checkout if they enter a ZIP code from the PIN codes listed in the code.

// Validate
function ts_woocommerce_after_checkout_validation( $data, $error ) {        
    // The accepted delivery zones
    $del_zones_array = array( 560043, 560038, 560025 );

    // If the postal is not within the array, deny checkout
    if( ! in_array( $data['shipping_postcode'], $del_zones_array ) ) {
        $error->add( 'validation', 'The ZIP you provided is not available for online deliveries.' );
    }
}
add_action('woocommerce_after_checkout_validation', 'ts_woocommerce_after_checkout_validation', 10, 2 );

Output

The below output shows that customers will be shown a validating message and also avoid proceeding with the order if the PIN Code is not in the predefined list of shipping postcodes.

How to Validate Shipping ZIP Code During Checkout in WooCommerce?

A similar error message can be shown when none of the shipping method is selected by the customer and instead tries to place an order just with the pre-selected shipping option. Refer to this post that will help you to make shipping method fields mandatory on WooCommerce checkout page.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x