• CrossLink - SEO Internal LinksCross Links

    Automatically cross link your Magento site. Great for SEO!

Create a Drop-Down of Countries

When I first needed to access a collection of countries in Magento I assumed it would work like all other data collections but was shocked to find that this wasn't the case. Rather than store country data in the database, Magento stores country data in an XML file and loads it in on each request. Fortunately though, there are some simple functions that we can use to access country names and codes in Magento.

Get An Array of Country Names/Codes in Magento

<?php

	$countryList = Mage::getResourceModel('directory/country_collection')
					->loadData()
					->toOptionArray(false);
	
	echo '<pre>';
	print_r( $countryList);
	exit('
'); ?>

The above code will print out an array containing every country code and country name known to Magento.

Drop Downs and Country Information

The most common reason developers access country names in Magento is to create a drop down. There are several ways to accomplish this and they differ depending on whether you're in the admin or the frontend.

Create a Country Drop Down in the Frontend of Magento

Add the following code to any template file in the frontend of Magento and you will get a drop down box using the country name as the label and the country code as the value.

<?php $_countries = Mage::getResourceModel('directory/country_collection')
									->loadData()
									->toOptionArray(false) ?>
<?php if (count($_countries) > 0): ?>
	<select name="country" id="country">
		<option value="">-- Please Select --</option>
		<?php foreach($_countries as $_country): ?>
			<option value="<?php echo $_country['value'] ?>">
				<?php echo $_country['label'] ?>
			</option>
		<?php endforeach; ?>
	</select>
<?php endif; ?>

Create a Country Drop Down in the Magento Admin

When creating forms in the Magento Admin area, it is very rare that we use actual HTML. The reason for this is that forms are generally built using pre-built functions. The benefit of this is that each Admin page looks uniform and helps to keep Magento looking like one whole application rather than having loads of bits stuck onto it. As our method of adding HTML changes, so must our method of creating our country drop down.

<?php

	$fieldset->addField('country', 'select', array(
		'name'	=> 'country',
		'label' 	=> 'Country',
		'values'	=> Mage::getModel('adminhtml/system_config_source_country')->toOptionArray(),	
	));

?>

15 thoughts on “Create a Drop-Down of Countries”

  • Roberto XSM

    Why don't you use RSS?

  • BT

    Apologies! RSS has been enabled now.

  • Andrew

    How can I tie this is with a state / province dropdown menu the way it's done with customer addresses?

  • Brigitte

    hallo,

    I will add a color dropdown in a grouped product table, maybe that here is my solution. Can you help me?? Can you tell me where I must include the Array Code?? And how must be the code for my color dropdown?? I would be verry thankful if you would help me

  • kiat

    The admin bit is exactly what I was after. Thanks!

  • Shahid

    Nice, at last I got it. I have been working on my form and coundnot get country code work, THanks for the help.

  • Shaaa

    Admin bit helped me a lot. Thanks :)

    @Andrew: Try following if your requirement is for admin

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

  • Dhanapal

    Super script.. It reduces its working and searching time dude.... Thanks for sharing

  • Gergely

    I was wondering if you can filter the country list based on the allowed countries in Configuration / General / Countries options? I can't seem to be able to access to those values. (Magento 1.4) Many thanks, G

  • Anulya

    Nice Shaa. It reduces so much work to me. Thanks a lot for sharing the states dropdown.

  • Gagan

    Hi, all

    Can i get cities drop down list in admin panel?

    Actually when i am trying to add a new order , it requires a new customer

    If i add a new customer i get the field as cities in the form of text area

    But i wanted it to be in drop down

    how can i get it in the admin panel in the backend??

  • Phil Maclachlan

    I was also wondering if you can filter the country list based on the allowed countries in Configuration / General / Countries options?

    We are unable to ship to certain countries due to laws or delivery restictions from courier... and ideally that would be best solution.

    Thanks
    Phil

  • Mounish Ambaliya

    i wanted to display State / Region's drop down Dynamically in Admin form based on country selection.

    Code above for :Create a Country Drop Down in the Magento Admin and State Drop Down works fine. But how to make it work dynamically,

    Like if i select India it shows states of india and if i select USA it shows states of Us and if i select any other country a text box should come...

  • Dhara

    Thanks! Worked like a charm! :)

  • Santosh Warang

    i want dynamic states drop down list when i select country drop down list then i want auto states of that country in lists....i am trying to that but i cant get so help me out guys.....

15 Item(s)

Comments