Magento : Create A Country Drop Down in the Frontend Template

Following is the magento code snippet used to create a country drop down in magento frontend template .phtml file:
Step 1. Create a function in your module helper class in .php file.
<?php
public function getCountryCollection()
{
return $countries = Mage::getResourceModel('directory/country_collection')
->loadData()
->toOptionArray(false);
}
?>

Step 2. Call this helper method in your .phtml file like below and show all countries in frontend.
<?php $_countries = Mage::helper("your_module")->getCountryCollection() ?>
<?php if (count($_countries) > 0): ?>
<select name="countries" id="countries">
<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; ?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s