How to Add Country States / Regions
We have some good news to share with you! In 2021 we will continue exploring Magento 2 together! Let's start with our first post of the year by finding out how you can add country States / Provinces / Regions for countries that Magento doesn't offer by default.
First of all, Happy New Year and wish you all the best for the new year!
In this post we will use an example our free Magento 2 extension, Greek Regions, which brings all regions for the country Greece. In Magento, we can find all data related with the regions, in the tables directory_country_region and directory_country_region_name. Table directory_country_region is for adding the regions we need and the table directory_country_region_name is for adding locale translations. In our example we add the regions of Greece in english and greek locale. Programmatically to do that we have created a data patch like below. As you can see, we have created an array with all values we need for every region, which is the country id, the region code and then the region name in English and Greek locale. First we need to add an entry to the table directory_country_region and the then with the region id we get back, we add also one entry in table directory_country_region_name for every region in the locale we need. That means you will need to know also the locale code which in our case the codes are the el_GR for Greek and en_US for English.
The full example as extension can be found here.
Feel free to share this post and ask your questions in the comments below.
Till next time!