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('</pre>');
?>
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(),
));
?>




9 Responses to Create a Drop-Down of Countries
Why don't you use RSS?
Posted on July 8, 2010 at 3:20 pm
Apologies! RSS has been enabled now.
Posted on July 8, 2010 at 3:57 pm
How can I tie this is with a state / province dropdown menu the way it's done with customer addresses?
Posted on August 17, 2010 at 4:54 pm
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
Posted on September 6, 2010 at 9:30 pm
The admin bit is exactly what I was after. Thanks!
Posted on November 4, 2010 at 2:33 pm
Nice, at last I got it. I have been working on my form and coundnot get country code work, THanks for the help.
Posted on June 20, 2011 at 6:34 pm
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,
));
Posted on September 14, 2011 at 5:26 pm
Super script.. It reduces its working and searching time dude.... Thanks for sharing
Posted on September 24, 2011 at 12:31 pm
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
Posted on September 29, 2011 at 6:13 pm
Nice Shaa. It reduces so much work to me. Thanks a lot for sharing the states dropdown.
Posted on December 28, 2011 at 10:46 am
Items 1 to 10 of 11 total