Magento – add regions to country
In my new article I show you how to add new regions to country. Per default there are only some regions in your Magento database. Primarily those from United States. Normally, it is not necessary to show regions or states during checkout, especially for small country. You will only need that, if you want to add different shipping costs to different regions of the same country. It is also possible by zip codes, but regions are by far the better way.
Add regions to country
In this tutorial I show you how to add regions for country South Africa. I suppose that you do not already have that in your shop. Regions are stored in two database tables namely:
- directory_country_region
it stores country_id (ISO country code), code of region and the full name of that region - directory_country_region_name
it stores by directory_country_region id all translations. You need language identifier like en_US and the specific translation of that region. For this example I only add default English version.
With this information in mind I created insert statements for all new regions:
1 2 3 4 5 6 7 8 9 | insert into directory_country_region (country_id, code, default_name) values ("ZA", "FS", "Free State"); insert into directory_country_region (country_id, code, default_name) values ("ZA", "GT", "Gauteng"); insert into directory_country_region (country_id, code, default_name) values ("ZA", "EC", "Eastern Cape"); insert into directory_country_region (country_id, code, default_name) values ("ZA", "NL", "KwaZulu-Natal"); insert into directory_country_region (country_id, code, default_name) values ("ZA", "LP", "Limpopo"); insert into directory_country_region (country_id, code, default_name) values ("ZA", "MP", "Mpumalanga"); insert into directory_country_region (country_id, code, default_name) values ("ZA", "NC", "Northern Cape"); insert into directory_country_region (country_id, code, default_name) values ("ZA", "NW", "North-West"); insert into directory_country_region (country_id, code, default_name) values ("ZA", "WC", "Western Cape"); |
For directory_country_region you have to fill the following fields:
- country_id
in Magento country_ids are stored by ISO country codes. Magento only uses country codes with two characters. - code
each region also uses a two character long code. - default_name
you have to set a default region name. This name is used if there are no translations.
You can safely run this SQL script with your prefered MySQL database client. The last step is no to add all translations:
1 2 3 4 5 6 7 8 9 | insert into directory_country_region_name values ("en_US", 507, "Free State"); insert into directory_country_region_name values ("en_US", 508, "Gauteng"); insert into directory_country_region_name values ("en_US", 509, "Eastern Cape"); insert into directory_country_region_name values ("en_US", 510, "KwaZulu-Natal"); insert into directory_country_region_name values ("en_US", 511, "Limpopo"); insert into directory_country_region_name values ("en_US", 512, "Mpumalanga"); insert into directory_country_region_name values ("en_US", 513, "Northern Cape"); insert into directory_country_region_name values ("en_US", 514, "North-West"); insert into directory_country_region_name values ("en_US", 515, "Western Cape"); |
Attention: you have to change id numbers. Look at your directory_country_region table and adjust all id numbers. If you don’t do that, you get wrong translation and a user might see a wrong region name in front end. The actual id depends on how much regions your Magento database already holds. That depends on Magento version and which language packs you have already installed.
For directory_country_region_name you can define localisations for region names. In this example I only created translations for en_US, with the same name as default, which is ambiguous.
Conclusion
If you ship to new countries it may be useful to add regions to Magento. This regions are used to make better shipping cost tables. It may also be necessary for your shipment partner to have this information. In Magento it is very easy to add additional regions and to add translations for all needed languages.
Awesome this works but in my example the shipping price is staying at 0. And not changing?
have you updated your tablerates for shipping costs?
Is there a ways to translate a Country’s name?