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.
    public function getCountryCollection()
        return $countries = Mage::getResourceModel('directory/country_collection')

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'] ?>
        <?php endforeach; ?>
<?php endif; ?>

Leave a Reply