How to filter and validate pincode in Indian woocommerce?

By default, Woocommerce only validates ZIP code of few countries only. Woocommerce stores in India, are thus invalidated in case of PINCODE inputs from customers. Here is how you can enable checking of pincode for India woocommerce:

Put the following code in your custom function file or child theme functions.php

add_filter('woocommerce_validate_postcode','validate_indian_postcode',10,3);

function validate_indian_postcode($valid, $postcode, $country){
	if($country=="IN")
			$valid = (bool) preg_match( '/^([0-9]{6})$/', $postcode );
  // checks your $postcode, if valid $valid will be true because of preg_match else false
  return $valid;
}

Reference:

https://github.com/woocommerce/woocommerce/blob/ef0f527b40dba539e982efff26211fa577a24cf9/includes/class-wc-validation.php#L44


Posted

in

,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *