Magento: Get country and region collection
If you have country code(e.g. NP, EN, NL) then you can get country name from the following code:
$countryName = Mage::getModel('directory/country')->load($countryCode)->getName();
Get the collection of all the 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 the collection of all the 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. NL, NP, EN) 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>
Related posts:
- Magento: Different shipping rate for different country and region
- Magento: Very Useful Collection Functions
- Magento: Set Random Order in Collection using RAND()
- Magento: How to filter product collection using 2 or more category filters?
- PHP MaxMind GeoIP: Get country, city, postal code & much more by IP Address
- Magento: Get Product Collection by Type
- Using jQuery & AJAX: Populate Selection List
- Magento: Redirect Customer to Login page if not logged in
- Magento: Block Controller Model Helper Override
- jQuery: Grey out background and preview image as popup
