Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

How to add Country dropdown list through module builder
 
Sharma Roshan
Jr. Member
 
Total Posts:  8
Joined:  2008-09-29
 

Hi All

I have create a new vendor module through module builder and want to add country drop down in it.

Please help me.

Thanks in Advance.

Regards,
Roshan Sharma

 
Magento Community Magento Community
Magento Community
Magento Community
 
Saggy
Sr. Member
 
Total Posts:  133
Joined:  2009-02-03
 

Have you figured it out? How to do this?

Cheers
S

 
Magento Community Magento Community
Magento Community
Magento Community
 
Herve Guetin
Sr. Member
 
Avatar
Total Posts:  216
Joined:  2008-05-10
Lyon, FR
 

Hey,

Digging out this quite old post to give my not-so-elegant code to do that.
Code to add in the /Block/Adminhtml/YOURMODULE/Edit/Tab/Form.php file :

$countryCollection Mage::getModel('directory/country_api')->items(); // Retrieves country collection
  
$value 1;
  foreach(
$countryCollection as $country// Build the array to inject into select
   
$countryList[$country['country_id']]['value'$value;
   
$countryList[$country['country_id']]['label'$country['name'];
   
$value $value 1;
  
}

  $fieldset
->addField('country''select', array(
          
'label'     => Mage::helper('mogashops')->__('Country'), // Your module name instead of "mogashops"
          
'name'      => 'country',
          
'values'    => $countryList// Use our previously built array.
  
));

If someone ever reads this post with a better solution using XML config, please post !
Hope this quick trick helps anyway.

 
Magento Community Magento Community
Magento Community
Magento Community
 
FishPig
Sr. Member
 
Avatar
Total Posts:  232
Joined:  2009-06-05
 

I’m not sure whether or not you found a better way but if not, check out the following article which shows how to do this in 1 line of code:

Get A List of Country Names/Codes for a Drop Down in Magento

 
Magento Community Magento Community
Magento Community
Magento Community
 
Herve Guetin
Sr. Member
 
Avatar
Total Posts:  216
Joined:  2008-05-10
Lyon, FR
 

Hey Fishpig,

Found this solution a while ago now and forgot I posted onto this topic smile
Thanks a lot for the answer.

Now the question is… how to update Region dropdown dynamically ?

Do you by chance have a clue ?

Thanks again smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
FishPig
Sr. Member
 
Avatar
Total Posts:  232
Joined:  2009-06-05
 

I’ve just had a quick look and found the following function in Mage_Checkout_Block_Onepage_Abstract.php.

<?php

    
public function getRegionCollection()
    
{
        
if (!$this->_regionCollection{
            $this
->_regionCollection Mage::getModel('directory/region')->getResourceCollection()
                ->
addCountryFilter($this->getAddress()->getCountryId())
                ->
load();
        
}
        
return $this->_regionCollection;
    
}

One way to dynamically change the region values depending on country would be to add the above function to a controller action and request that action using AJAX. You could then have the action return the regions in JSON format and use jQuery to populate the region drop down

Would that work?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Herve Guetin
Sr. Member
 
Avatar
Total Posts:  216
Joined:  2008-05-10
Lyon, FR
 

Maybe a bit of changing this code would do the trick to get the data. Like giving an argument in getCountryId() which would be the country selected in the country drop-down. But using jQuery here is, I think, not the best thing to do as Magento has a built-in AJAX system for the backend which I don’t really know how to use…

 
Magento Community Magento Community
Magento Community
Magento Community
 
FishPig
Sr. Member
 
Avatar
Total Posts:  232
Joined:  2009-06-05
 

The backend of Magento uses Prototype so it would be best to code it with that. If you couldn’t do that though, you could include the jQuery library just on that one page. This would slow things down a little but at least you would achieve what you’re trying to do.

The function given should be put into your custom module and then you would specify the country ID as a parameter.

I would recommend looking at exactly how the onepage checkout does this. Use a Firefox plugin such as HTTPfox and you should get your answers.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nabler
Sr. Member
 
Total Posts:  80
Joined:  2010-07-27
 

so how to change the country spell?…
ex:Danmark -> Denmark

 
Magento Community Magento Community
Magento Community
Magento Community
 
FishPig
Sr. Member
 
Avatar
Total Posts:  232
Joined:  2009-06-05
 

Assuming you have your locale set to Danish:

Create the following file, replacing package and theme for the names of your package and theme.

app/design/frontend/package/theme/locale/da_DK/translate.csv

Inside this file add the following:

“Denmark”,"Danmark"
“EnglishWord”,"DanishWord"

This document uses the syntax:

“WordToTranslate”,"TranslatedWord"
“WordToTranslate2”,"TranslatedWord2"

If you aren’t using the Danish language pack and just want to change Denmark to Danmark on a site that has all English text, you would change da_DK to en_US or en_GB (or which language pack you’re using).

I hope this makes sense!

 
Magento Community Magento Community
Magento Community
Magento Community
 
parvezmac1
Jr. Member
 
Total Posts:  4
Joined:  2010-07-17
noida
 

Hi you can use below snippet to get country list.

$countryList Mage::getResourceModel(‘directory/country_collection’)  
->
loadData()  
->
toOptionArray(false);  
echo 
‘<pre>’;  
print_r$countryList);  
exit(
‘</pre>’);

Reference from http://www.phpflow.com

 
Magento Community Magento Community
Magento Community
Magento Community
 
shuks
Member
 
Total Posts:  42
Joined:  2010-07-21
 

This should be exactly what everyone is looking for. It worked great for me…

http://xhtmlandcsshelp.blogspot.ca/2012/07/how-to-add-country-and-state-dropdown-in-magento-admin-using-ajax.html

 
Magento Community Magento Community
Magento Community
Magento Community
 
TutorialMagento
Sr. Member
 
Avatar
Total Posts:  133
Joined:  2013-10-02
 

Hi, I think you don’t need to use a separate module, you can follow this tutorial to add some code to your site to create a Country Dropdown list
http://tutorialmagento.com/ho-to-create-a-drop-down-list-of-countries-in-magento
Hope this helps

 
Magento Community Magento Community
Magento Community
Magento Community
 
ukashkartim
Jr. Member
 
Total Posts:  3
Joined:  2014-05-02
 

i hv a same problem too

thanks TutorialMagento

your solution resolve the problem

_____________________________________________________________________________________________________________
UKASH - UKASH

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top