• Resolved A.m

    (@ameliaakh)


    There is an error when entering a postal code in Belgium. It is possible to enter an incorrect e.g. the Netherlands zip code (1011AM) and choose the country Belgium, no error will be displayed. In Belgium postal codes 1011AM are invalid but no error is displayed. How can this be corrected, is it theme related or is it woocommerce related?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Hari Shanker R

    (@harishanker)

    Hi @ameliaakh

    I understand the problem that you’re talking about. Can you share more details about how to replicate the error? What was the zip code (or the format you entered) so as to get the error message? The reason why I asked is that

    I was able to replicate the problem with the Netherlands zip code NOT showing an error when Belgium is chosen as the country though.

    Now, WooCommerce has some basic postcode validation built-in to core. However, if you would like to implement something advanced, you might want to consider something like this:
    https://woocommerce.com/products/postcodeaddress-validation/
    https://wordpress.org/plugins/woo-address-validator/

    I hope this helps!

    Thread Starter A.m

    (@ameliaakh)

    Hello @harishanker
    “I was able to replicate the problem with the Netherlands zip code NOT showing an error when Belgium is chosen as the country though.”

    Yes this is exactly the problem, postal code of Belgium consists of 4 digits with no letters, there should be an error if someone uses a zip code that contains more than 4 digits. I think this is basic, if I choose Netherlands as country and will input 4 digits, it will show error, the same should be applied to Belgium, but in this case anything more than 4 digits should be an error. I think it’s not advanced? I would prefer to not make my shop heavier by adding more plugins Hari, is there a chance that WooCommerce will correct this for Belgium in near future?
    With kind regards, Amelia

    Moderator Hari Shanker R

    (@harishanker)

    Hi @ameliaakh

    I hear you. It’s a bummer that the Belgium zip/postal code validation is not available in the WooCommerce core by default.

    I checked WooCommerce core and found the relevant lines of code that adds this feature: https://github.com/woocommerce/woocommerce/blob/d20d429e3264798fbfd5335e971f1e46531cf7bb/includes/class-wc-validation.php#L40-L108

    As seen here, the woocommerce_validate_postcode hook can be used to manipulate these validation rules.

    Based on this, I prepared a quick code snippet that should help you in achieving your requirement:

    add_filter('woocommerce_validate_postcode','woo_validate_belgium_postcode',10,3);
     
    function woo_validate_belgium_postcode($valid, $postcode, $country){
        if($country=="BE")
                $valid = (bool) preg_match( '/^([0-9]{4})$/', $postcode );
      // Checks your postcode to seee if it matches belgium (4 digits). If not, it will reject validation.
      return $valid;
    }

    I’d recommend adding it to your site using a Code Snippets Plugin, even though you can always add code snippets like this to the bottom of your theme’s functions.php. (The downside of this approach is that your code gets lost in plugin/theme updates).

    When you add this PHP code to your site, WooCommerce will only accept four-digit zip codes when you select Belgium as the country. As you can see in the following screenshot, it rejects the 1011AM zip code from your example: https://d.pr/i/J3ci41

    I hope this helps.

    Thread Starter A.m

    (@ameliaakh)

    @harishanker Thank you very much Hari, this is so nice. I added the code and it works! Thanks again for your help!
    Wish you a great day,
    Amelia

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Bug in zip code’ is closed to new replies.