Magento- Create a Drop-Down of Countries

April 9th, 2012 by laeeq | 2 comments

For getting countries data, We first needed to access a collection of countries in Magento I assumed it would work like all other data collections but was surprised 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

  1. <?php
  2. $countryList = Mage::getResourceModel(‘directory/country_collection’)
  3. ->loadData()
  4. ->toOptionArray(false);
  5. echo ‘<pre>’;
  6. print_r( $countryList);
  7. exit(‘</pre>’);
  8. ?>

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

To get 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.

  1. <?php $_countries = Mage::getResourceModel(‘directory/country_collection’)
  2. ->loadData()
  3. ->toOptionArray(false) ?>
  4. <?php if (count($_countries) > 0): ?>
  5. <select name=“country” id=“country”>
  6. <option value=“”>– Please Select –</option>
  7. <?php foreach($_countries as $_country): ?>
  8. <option value=“<?php echo $_country['value'] ?>”>
  9. <?php echo $_country['label'] ?>
  10. </option>
  11. <?php endforeach; ?>
  12. </select>
  13. <?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.

  1. <?php
  2. $fieldset->addField(‘country’, ‘select’, array(
  3. ‘name’ => ‘country’,
  4. ‘label’ => ‘Country’,
  5. ‘values’ => Mage::getModel(‘adminhtml/system_config_source_country’)->toOptionArray(),
  6. ));
  7. ?>

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Get an array of billing addresses and shipping addresses in Magento
  • Duplicate Data Issue with Custom Magento Collection
  • Tips To Improve Your Ecommerce Site’s Security
  • Magento: Display New Products from Specific Category On Home Page
  • Dealing with Magento Model or Collections
  • Magento: Display New Products On Home Page
  • Magento: Display More Than 5 New Products
  • Override Controllers in Magento
  • Improve the performance of your Magento store
  • Magento Released Community Edition 1.7.0.1
  • Write custom title, keywords and description in Magento module
  • Moving Magento site from development to live server
  • Display Related products on product details page in Magento
  • Magento – Models, resource models, and collections
  • Magento – Add thumbnail images to Magento admin grid
  • Simple url rewrite in magento
  • Generate CSV file in Magento
  • Resize image in Magento using Varien_Image class
  • AJAX requests in Magento
  • Category Navigation Listings in Magento
  •  

     

    1. jasongrayzg
      September 28th, 2012 at 18:19 | #1

      Keep up the good piece of work, I read few articles on this site and I believe that your weblog is rattling interesting and contains circles of good information.

    2. October 7th, 2012 at 15:26 | #2

      Hi, I do think this is a great blog. I stumbledupon it ;
      ) I will revisit once again since I bookmarked it.
      Money and freedom is the best way to change,
      may you be rich and continue to guide others.

    1. No trackbacks yet.