search
top

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 Responses to “Country and States in Magento”

  1. slavisha says:

    This is great, just what I needed! thanks

  2. Shaaa says:

    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,
    ));

  3. Shaaa says:

    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

top