2

We are running Magento 2.1.6. We want to set predefined regions and areas as drop down lists in addresses, just like when you select country us, you have the states pre selected, etc..

For example, we want to add when people select UAE as the country to have the region field in address prepopulated with the 7 states in the UAE, and if for example the user select the state Dubai, we want to have a Area prepopulated with all the different areas known in Dubai what we would populate manually.

Any thoughts on how to do this ?

3

In order to show the region fields should be shown for non required state :

(can be configured from Stores / Configuration / General / State Options / Allow to Choose State if it is Optional for Country)

vendor/magento/module-directory/Helper/Data.php:268

/*
 * Path to config value, which detects whether or not display the state for the country, if it is not required
 */
const XML_PATH_DISPLAY_ALL_STATES = 'general/region/display_all';

/**
 * Return, whether non-required state should be shown
 *
 * @return bool
 */
public function isShowNonRequiredState()
{
    return (bool)$this->scopeConfig->getValue(
        self::XML_PATH_DISPLAY_ALL_STATES,
        ScopeInterface::SCOPE_STORE
    );
}

In order to set the region required for a country :

(can be configured from Stores / Configuration / General / State Options / State is Required for)

vendor/magento/module-directory/Helper/Data.php:281

/*
 * Path to config value, which lists countries, for which state is required.
 */
const XML_PATH_STATES_REQUIRED = 'general/region/state_required';

/**
 * Returns flag, which indicates whether region is required for specified country
 *
 * @param string $countryId
 * @return bool
 */
public function isRegionRequired($countryId)
{
    $countyList = $this->getCountriesWithStatesRequired();
    if (!is_array($countyList)) {
        return false;
    }
    return in_array($countryId, $countyList);
}

Then to add a new country and region :

  1. Add a now country inside this table : directory_country
  2. Add related region inside this table : directory_country_region related with the country_id column.
  3. Add translated version of your region in this table : directory_country_region_name

Add these information from a setup script. You can based on the Magento Directory Setup Data : vendor/magento/module-directory/Setup/InstallData.php:17

1

You can use the direct SQL files to add regions into database. For this you can refer this post.

Another way is You can use the Magento custom code to add regions into database. For this you can refer this post.

Also I have found one extension "Regions Manager" that provide facility to add/update regions from backend.

  • Thanks, but the post is regarding magento 2, we are running 2.1.6 – Fodakty Apr 18 '17 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.