Adding County / Regions / States / Provinces in Magento to Include Dropdown in Cart
By default, Magento only provides a list of states (regions/provinces) for the United States. US clients only make up about 12% of our total client base so we’re forever having to import a list of counties / regions / states / provinces (we’ll just refer to them as regions for the benefit of this post) for the relevant countries that our client sells to. More commonly for us, is the UK (seeing as this is where our headquarters are).
Some of our clients may prefer to display a dropdown of regions for the relevant country at cart/checkout and it can also be easier to set shipping rules in your table rates by region if you have these predefined available in a dropdown.
Whilst Magento provides no direct way to import these in the administration back office, you can access your database directly via your database interface (e.g. phpMyAdmin) and import a CSV of the regions directly into the database which will then populate these for that country in Magento.
There are two tables in the database that house this information:-
- directory_country_region
- directory_country_region_name
The table directory_country_region contains all the region codes along with their region id’s whilst the directory_country_region_name provides the translation for your set locale by using the region id and the locale you want to provide the translation for.
The directory_country_region table will look like this:-
Note that the country_id must use the official ISO-2 (alpha-2) two letter country codes defined in ISO 3166-1 and the region code must be the official codes from ISO 3166-2 (which you can retrieve by clicking on the link for that column in the table on the linked page above.
The directory_country_region_name table will look like this:-
You can obtain a list of locales here if you are unsure of what the code is (or a quick search online should give you the code).
You can download the CSV’s to import into your Magento database below (we will try and add regions for more countries as and when we can although feel free to leave a comment if you would like a list to import for a particular country not listed).
India (IN)
directory_country_region_IN
directory_country_region_name_IN
United Kingdom (GB)
directory_country_region_GB
direct_country_region_name_GB
There is actually a free extension in the Magento Connect marketplace that provides you with the ability to add, edit and remove regions directly from your Magento back office which proves far safer that direct imports in to the Magento database. We have never used the extension though so cannot guarantee it does everything correctly. If you are familiar and have experience with it, then please let us know in the comments.
Extension you mentioned is for Magento 1.9 en below, so, the more ssecure way for Magento 2 is inserting directly in database as I have already done. The question I have is: by mean of what API call can I retrieve regions by country filter?