Saturday, January 22, 2011

PrestaShop Tips - State iso_code length problem in PrestaShop (ver 1.3.2.3 and older)

If you are using PrestaShop and ship to country with state, and the state iso code is long than 4, or has numeric character in the state iso code, you will encounter an issue. Because in PrestaShop, it only allows you input max 4 characters and alpha character A to Z or a to z only.

Problems


Not able to input iso_coe longer than 4 character
Not able to input iso_code with none-numeric character
For example, Japanese "state"(prefecture) iso_code are as following

# JP-23 Aiti (Aichi)
# JP-05 Akita
# JP-02 Aomori
# JP-38 Ehime
# JP-21 Gihu (Gifu)
# JP-10 Gunma
.......

Versions of PrestaShop have this issue
1.3.2.3 and older versions of PrestaShop have this issue.
But the latest version of 1.4 Beta 5 also have this issue at time of this post.

Solutions


There are two solutions to above problems.

Solution 1
First solution is kind of walk around.

PrestaShop is not using the state ISO CODE as a key, so it doesn't matter you input correct iso code (for state) or not. So you can use "JPAI" as ISO code of Aiti instead of JP-23. It really doesn't matter. (I think this way is much more user friendly when display on UI, even it feels a little weird). So Here is my suggestion.

# JPAI Aiti (Aichi)
# JPAK Akita
# JPAO Aomori
# JPEH Ehime
# JPGI Gihu (Gifu)
# JPGU Gunma
.....

Solution 2


Here I introduce a proper way to fix this issue. By following below instructions, you will be able to fix the issue by code, instead of using FAKE iso code as walk around.

Note: following code and line# is based on PrestaShop 1.3.2.3, it maybe a slight different for other versions.

1. Change length iso_code field in database table ps_state to 5
(You can use any database tools, such as PHPMyAdmin)

2. Change length of iso_code length validation in file /classed/State.php, line #37 (PS 1.3.2.3)

From


protected $fieldsSize = array('iso_code' => 4, 'name' => 32);


To


protected $fieldsSize = array('iso_code' => 5, 'name' => 32);



3. Change the regular express of iso_code in file /classes/Validation.php, line #258 (PS1.3.2.3)

From


static public function isStateIsoCode($isoCode)
{
return preg_match('/^[a-z]{1,4}$/ui', $isoCode);
}


To


static public function isStateIsoCode($isoCode)
{
return preg_match('/^[a-z0-9-]{1,5}$/ui', $isoCode);
}


4. Finnaly, change the length input textbox in admin/tabs/AdminStates.php file, line #107


<input type="text" size="5" maxlength="5" name="iso_code" value="'.htmlentities($this->getFieldValue($obj, 'iso_code'), ENT_COMPAT, 'UTF-8').'" style="text-transform: uppercase;" /> <sup>*</sup>
<p>'.$this->l('1 to 5 letter ISO code').' (<a href="http://simple.wikipedia.org/wiki/List_of_U.S._states" target="_blank">'.$this->l('official list here').'</a>)</p>


After finish all above changes, you should be able to input 5 characters length iso code with numeric and "-" now.

I prefer solution 2 as a developer, because it fixes the issue completely. For store owners don't know how to code at all, I suggest to use solution 1. But you can also challenge yourself, because this is not a difficult changes.

No comments: