Home » Magento

Magento: Get country and region collection

9 December 2009 1,349 views One Comment Popularity: 15% Share/Bookmark

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>
 

From Mukesh Chapagain's Blog | Post Magento: Get country and region collection

Related posts:

  1. Magento: Set Random Order in Collection using RAND()
  2. Magento: Very Useful Collection Functions
  3. Magento: How to filter product collection using 2 or more category filters?
  4. Magento: How to get attribute name and value?
  5. Magento: Get sub categories and product count

One Comment »

  • key said:

    i want to use admin grid modify region, add edit or delete, could you gei me some advices.

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.