Country and States in Magento

Hello All,

Magento has inbuilt model for Country and State as seen in Checkout page.

In checkout page if you select Country like ‘France’ , a drop down will be seen in place of textbox for States.

I am writing here the code to directly get country name from ID and also to get States info from country.

<?php
	$countryName = Mage::getModel('directory/country')->load('FR')->getName(); //get country name

	
	echo 'Country Name ->'.$countryName.'<br/>';


	$states = Mage::getModel('directory/country')->load('FR')->getRegions();

	

	//state names

	foreach ($states as $state)

	{		

		echo 'ID->'.$state->getId().'<br/>';
		echo 'Name->'.$state->getName().'<br/>';

	}
?>

Also checkout this post Get List of Countries in Magento

Hope this is useful.

4 thoughts on “Country and States in Magento

  1. I found the solution,

    $stateCollection = Mage::getModel(‘directory/country’)->load(‘US’)->getRegions()->toOptionArray();

    $fieldset->addField(‘state’, ‘select’, array(
    ‘label’ => Mage::helper(‘studentcenter’)->__(‘State’),
    ‘required’ => true,
    ‘name’ => ‘state’,
    ‘values’ => $stateCollection,
    ));

  2. How can i use this for admin module form?

    Where a drop down box for state is applied like follows.

    $fieldset->addField(‘state’, ‘select’, array(
    ‘label’ => Mage::helper(‘studentcenter’)->__(‘State’),
    ‘required’ => true,
    ‘name’ => ‘state’,
    ‘values’ => array(
    array(
    ‘value’ => ”,
    ‘label’ => Mage::helper(‘studentcenter’)->__(‘– Select State –‘),
    ),
    ),
    ));

Leave a Reply to Shaaa Cancel reply

Your email address will not be published. Required fields are marked *

Scroll to top