Home » Magento

Magento: Get country and region collection

9 December 2009 7,690 views Popularity: 16% Share/Bookmark

email

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:

  1. Magento: Different shipping rate for different country and region
  2. Magento: Very Useful Collection Functions
  3. Magento: Set Random Order in Collection using RAND()
  4. Magento: How to filter product collection using 2 or more category filters?
  5. PHP MaxMind GeoIP: Get country, city, postal code & much more by IP Address
  6. Magento: Get Product Collection by Type
  7. Using jQuery & AJAX: Populate Selection List
  8. Magento: Redirect Customer to Login page if not logged in
  9. Magento: Block Controller Model Helper Override
  10. jQuery: Grey out background and preview image as popup
  • http://www.hellokeykey.com key

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

  • http://haijerome.wordpress.com Jerome

    Hi,

    Thanx a lot !!! It saved my time !!! And also i found this blog is very helpful for magento beginners like myself. It turned out to be saviour for myself.All the posts regarding magento are really very handy during development. So my hearty appreciations , Kudos to you and kindly continue this good work.

    Best Regards,
    Jerome Dennis D

  • Parvezmca

    how to get country ‘name’ waise.here display country code wise.

  • http://www.aligent.com.au Jonathan Day

    Thanks Mukesh, here is another useful one:

    Mage::getResourceModel(‘directory/country_collection’)->load()->toOptionArray();

    That will give you a list of all countries in the correct structure for a html select drop-down. Useful in frontend and adminhtml forms.

    Cheers,
    JD

  • Pranalid15

    But how  can I get the country code $countryCode

  • Pritesh Modi

    In product import script if i found new country then how can i add new country by script??

  • http://www.pctrickers.com Sumith Harshan

    thanks really worked!!!
    Here is another mothod
    Visit http://www.sumithnet.com

    *__(‘Country’) ?>
    loadData()
    ->toOptionArray(false) ?>
    0): ?>

    — Please Select –

    <option value="”>

  • Sunil830_talekar2007

    Great Work Mukesh Thanks