LaughingHorse Posted December 28, 2015 Share Posted December 28, 2015 We just got a call from a customer who had entered their information into the shopping cart.He had entered his zip code as (I'm making up numbers here) 12345 1234And he got an error that there were no shipping methods available.Looking at his address I spotted there was no "-" inbetween his zip code and the +4 numbers.After I went into his account and removed the space and +4 numbers of his zip code, he was able to complete his order.If a customer is using a US zip code and wants to use the +4 numbers, they need to enter the "-"Out of curiosity, does a space inbetween numbers like in Canadian zip codes also cause an error saying there is no shipping method available? Quote Link to comment Share on other sites More sharing options...
bsmither Posted December 28, 2015 Share Posted December 28, 2015 We would like to know what shipping module(s) you have enabled.The USPS module has:if(preg_match('/^([0-9]){5}-([0-9]){4}$/',$delivery['postcode'])) { $delivery['postcode'] = substr($delivery['postcode'],0,5); }which specifies a dash. Thus, no dash, no match. No match, no using just the first five numbers.I will concur with your assessment that the entry should be stripped of all characters not a digit, then using only the first five in the rate request.According to the USPS module for International shipments, the rate request just uses the country ISO code. Quote Link to comment Share on other sites More sharing options...
LaughingHorse Posted December 28, 2015 Author Share Posted December 28, 2015 I have USPS 1.1.1, and UPS 1.0 modules enabled. Quote Link to comment Share on other sites More sharing options...
bsandall Posted December 28, 2015 Share Posted December 28, 2015 (edited) Perhaps a better pattern would be '/^([0-9]){5}(-| )?(([0-9]){4})?$/'- this would match 12345, 123456789, 12345-6789, and 12345 6789.EDIT: However, reviewing the UPS developer's guide:If Shipper country is US or Puerto Rico, 5 or 9 digits are required. . The character - may be used to separate the first five digits and the last four digits. If the Shipper country is CA, then the postal code is required and must be 6 alphanumeric characters whose format is A#A#A# where A is an uppercase letter and # is a digit. For all other countries the postal code is optional and must be no more than 9 alphanumeric characters long.Neither of the above patterns is sufficient to meet the specification, and neither one automatically inserts a hyphen; though the API does say that a hyphen is optional, it doesn't specify whether a space is allowed.EDIT 2: Interestingly, the standard UPS module doesn't show any available shipping options for 9-digit codes with or without a hyphen included. It seems it can only accept exactly 5 digits, even though the API docs suggest that 9 digits should be acceptable. Edited December 28, 2015 by bsandall Added testing results Quote Link to comment Share on other sites More sharing options...
bsandall Posted December 28, 2015 Share Posted December 28, 2015 Here's a little code snippet that would meet the UPS specs, at least: /** * Validates and formats the postal code based on the country following UPS specifications. * @param $postal_code the postal code to validate * @param $country_code the 2-letter country code * @throws InvalidArgumentException if the code is invalid * @return formatted postal code */ public function getFormattedPostalCode($postal_code, $country_code) { switch ($country_code) { case 'PR': // fall-through to US case 'US': // US and PR (Puerto Rico): 5 or 9 digits in the format xxxxx-xxxx $pattern = '/^([0-9]){5}(-| )?(([0-9]){4})?$/'; if (empty($postal_code) || !preg_match($pattern, $postal_code)) { throw new \InvalidArgumentException("Invalid postal code: $postal_code"); } if (strlen($postal_code) > 5 && $postal_code[5] != '-') { $postal_code = substr($postal_code, 0, 5) . '-' . substr($postal_code, -4); } return $postal_code; case 'CA': // CA (Canada): A#A#A# - upper case letter-digit-letter-digit-letter-digit $postal_code = strtoupper($postal_code); $pattern = '/^([A-Z][0-9]){3}$/'; if (empty($postal_code) || !preg_match($pattern, $postal_code)) { throw new \InvalidArgumentException("Invalid postal code: $postal_code"); } return $postal_code; default: // All other countries: code is optional; if present, may be up to 9 alphanumeric characters $pattern = '/^[a-z0-9]$/i'; if (!empty($postal_code) && (strlen($postal_code) > 9 || !preg_match($pattern, $postal_code))) { throw new \InvalidArgumentException("Invalid postal code: $postal_code"); } // postal code returned below } return $postal_code; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.