Inaccurate ZIP codes are costing you money! Did you know that if a customer inputs an incorrect zip code, then it’s not just them getting charged incorrectly but also affects your WooCommerce store as well?

The number of digits in a customer’s zip code in the WooCommerce checkout page can have an effect on their tax liability and could be costing you. Customers may submit an incorrect entry by accident if they are not restricted from entering more than a required number of digits.

We had a similar problem when we were calculating TAX based on the ZIP code in one of our client’s WooCommerce shop. The customer’s billing and shipping address was US-based, but some mistakenly inputted 6 digits instead of 5 for their zip code; this caused orders to be successful yet taxes weren’t calculated correctly.

We are going to show you how did we resolve this issue and limited the number of digits in the billing and shipping ZIP code field on WoCommerce checkout page.

This is just a tiny bit of code that you need to place in your theme’s funnctions.php file. First, we are going to limit the number of digits to 5 for the billing address zip code field below.

function cdxn_wc_custom_billing_fields( $fields ) {
    // restricting the number of allowed digits to 5 for the billing postcode field
    $fields['billing_postcode']['maxlength'] = 5;
    return $fields;
}
add_filter( 'woocommerce_billing_fields', 'cdxn_wc_custom_billing_fields' );

And after that, we are going to limit the number of digits to 5 for the shipping address zip code field as below.

function cdxn_wc_custom_shipping_fields( $fields ) {
    // restricting the number of allowed digits to 5 for the shipping postcode field
    $fields['shipping_postcode']['maxlength'] = 5;
    return $fields;
}
add_filter( 'woocommerce_shipping_fields', 'cdxn_wc_custom_shipping_fields' );

That’s all. You need to copy and paste the above code and place them in your theme’s functions.php file.

We hope this tutorial was an enjoyable and easy learning experience for you. Here are some more WooCommerce tutorials that may interest or help with your store! You’re also welcome to visit our blog page

Happy coding!
Thank you