Create a Drop-Down of Countries

Posted on July 5, 2010 by BT There have been 13 comment(s)

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

?>

This post was posted in Magento Tutorials and was tagged with Magento Tips

13 Responses to Create a Drop-Down of Countries

  • Roberto XSM

    Why don't you use RSS?

    Posted on July 8, 2010 at 3:20 pm

  • fishpig
    fishpig says:

    Apologies! RSS has been enabled now.

    Posted on July 8, 2010 at 3:57 pm

  • Andrew
    Andrew says:

    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

  • 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

    Posted on September 6, 2010 at 9:30 pm

  • kiat
    kiat says:

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

    Posted on November 4, 2010 at 2:33 pm

  • Shahid
    Shahid says:

    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

  • Shaaa
    Shaaa says:

    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

  • Dhanapal

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

    Posted on September 24, 2011 at 12:31 pm

  • Gergely
    Gergely says:

    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

  • Anulya
    Anulya says:

    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

  • Gagan
    Gagan says:

    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??

    Posted on January 18, 2012 at 7:07 am

  • 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

    Posted on March 7, 2012 at 10:23 am

  • 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...

    Posted on April 11, 2012 at 11:25 am

13 Item(s)

Have your say...

You must be logged in to post a comment.