Woocommerce Checkout Zip code or Pin code Field Validation


In case you receive lot of incorrect zip code, postal code or pin code entered in the billing or shipping address fields of woocommerce powered ecommerce website, you can limit the zip code field digits to validate it properly. For instance if in your country zip code is of minimum 4, 5, or 6 digits, then you can use woocommerce checkout process function add_action code to validate it for correct zip code entry by your customers.
Here is the code to use if you want to validate the zip code field in checkout process of woocommerce:

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
  
function my_custom_checkout_field_process() {
    global $woocommerce;
  
    // Check if set, if its not set add an error. This one is only requite for companies
    if ( ! (preg_match('/^[0-9]{6}$/D', $_POST['billing_postcode'] ))){
        wc_add_notice( "Incorrect Zip code! Please enter correct number."  ,'error' );
    }
}
Woocommerce Checkout Zip code or Pin code Field Validation Woocommerce Checkout Zip code or Pin code Field Validation Reviewed by webmission on 13:30 Rating: 5

No comments:

Powered by Blogger.