Home » Magento9 December 2009

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>
 

From Mukesh Chapagain's Blog, post Magento: Get country and region collection

php magento mukesh chapagain

Get New Post by Email

Find me on

Facebook Twitter Google+ LinkedIn RSS Feed
  • 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

  • josh

    Returns erros to me when I use ‘public’. Whats going on?

  • Bhagwati

    use following code to the billing.php under core folder.

    public function getRegionCollection($countryCode)
    {
    $regionCollection = Mage::getModel(‘directory/region_api’)->items($countryCode);
    return $regionCollection;
    }