Magento: Get country and region collection

This article shows how to get all the country list and region list in Magento and then populate that list in a selection box.

Before going the code for fetching countries and regions, I would like to show a quick code to get country name by country code. Country code can be like NP (for Nepal), IN (for India), NL (for Netherlands), GB (for United Kingdom), US (for United States), etc.

Get country name by country code


$countryCode = 'NP';
$countryName = Mage::getModel('directory/country')->load($countryCode)->getName();
// outputs: Nepal

Get collection of all countries


/**
 * Get country collection
 * @return array
 */
public function getCountryCollection()
{
    $countryCollection = Mage::getModel('directory/country_api')->items();
    return $countryCollection;
}

Populate selection list with the country collection


$countryCollection = $this->getCountryCollection();

<select name='customer[country_id]' id='customer:country_id' class="validate-select" >
    <?php
        foreach($countryCollection as $country) {
            ?>
            <option value="<?php echo $country['country_id'] ?>" ><?php echo $country['name'] ?></option>
            <?php
        }
    ?>
</select>

Get collection of all states/regions related to specific country


/**
 * Get region collection
 * @param string $countryCode
 * @return array
 */
public function getRegionCollection($countryCode)
{
    $regionCollection = Mage::getModel('directory/region_api')->items($countryCode);
    return $regionCollection;
}

Populate region list with region collection

Country code (e.g. NP, IN, NL, GB, US, etc.) is passed as parameter to getRegionCollection function.


$regionCollection = $this->getRegionCollection($countryCode);

<select name='customer[region]' id='customer:region' class="validate-select" >
    <option>Please select region, state or province</option>
    <?php
        foreach($regionCollection as $region) {
            ?>
            <option value="<?php echo $region['name'] ?>" ><?php echo $region['name'] ?></option>
            <?php
        }
    ?>
</select>

Hope it helps. Thanks.