Create a drop-down list of country in Magento frontend and backend

0

When working with Magento, for some specific reasons a developer want to access list of countries to add a drop-down list of country to both front-end and back-end of Magento. How to do this?

create a drop list of country in magento

In this tutorial we will go through some points:

  • Access and get list of Country Names/Codes in Magento
  • Add a dropdown list of country to back-end of Magento
  • Add a dropdown list of country to front-end of Magento

This method was tested and worked properly in Magento 1.8 and 1.9

Access and get list of Country Names/Codes in Magento

Unlike other platform, Magento store countries data in an XML file and it will load this XML file on each request. We can use the following code to print an array of Country data (Name and code).

Now we got the data, next we will learn how to add a drop-down list box to Magento (backend and frontend)

Add Drop-down list box to backend

Based on the array we got from step 1, we can use the following code to add drop-list box to Magento backend. We will use PHP to ensure Magento admin pages look neat and uniform.

Add Drop-down list box to front-end of Magento

For front-end, find the layout file you want to add the drop-down list box to, for example, we will add drop-down list box to Header of your site.

Go to  app/design/frontend/rwd/default/template/page/html/header.phtml edit and add the following code:

create a drop list of country in magento frontend

Save changes and reload your homepage too see changecreate a drop list of country in magento

I hope you love this quick and sweet tutorial.

Rate this post

Share.

About Author

Brian is a Magento developer with 5 years of experience with customizing Magento extensions, Magento templates. He's main author at Magento explorer

Leave A Reply