Monday , December 11 2017
Home >> All tutorial >> How to Add country and state Dropdown in magento admin

How to Add country and state Dropdown in magento admin

I din’t able to create this in proper way like magento does but if you will have all the state of all country then I think this is the perfect solution. If you don’t have all state for all country then this module is not solved your problem. If any one knows the correct way then please add the solution via comment. I am describing here How I exactly did.

Open your form which is in Yournamespace/Modulename/Block/Adminhtml/Modulename/Edit/Tab/Form.php then add below fields

  1. $country = $fieldset->addField(‘country’, ‘select’, array(
  2.             ‘name’  => ‘country’,
  3.             ‘label’     => ‘Country’,
  4.             ‘values’    => Mage::getModel(‘adminhtml/system_config_source_country’) ->toOptionArray(),
  5.             ‘onchange’ => ‘getstate(this)’,
  6.         ));
  7.  
  8. $fieldset->addField(‘state’, ‘select’, array(
  9.             ‘name’  => ‘state’,
  10.             ‘label’     => ‘State’,
  11.             ‘values’    => Mage::getModel(‘modulename/modulename’)
  12.                             ->getstate(‘AU’),
  13.         ));
  14.  
  15.          /*
  16.          * Add Ajax to the Country select box html output
  17.          */
  18.         $country->setAfterElementHtml(“<script type=\”text/javascript\”>
  19.             function getstate(selectElement){
  20.                 var reloadurl = ‘”. $this
  21.                  ->getUrl(‘modulename/adminhtml_modulename/state’) . country/ + selectElement.value;
  22.                 new Ajax.Request(reloadurl, {
  23.                     method: ‘get’,
  24.                     onLoading: function (stateform) {
  25.                         $(‘state’).update(‘Searching…’);
  26.                     },
  27.                     onComplete: function(stateform) {
  28.                         $(‘state’).update(stateform.responseText);
  29.                     }
  30.                 });
  31.             }
  32.         </script>“);
Now Create State Action in modulenamecontroller.php file which will be like this
  1.  public function stateAction() {
  2.         $countrycode = $this->getRequest()->getParam(‘country’);
  3.         $state = “<option value=”>Please Select</option>”;
  4.         if ($countrycode != ) {
  5.             $statearray = Mage::getModel(‘directory/region’)->getResourceCollection() ->addCountryFilter($countrycode)->load();
  6.             foreach ($statearray as $_state) {
  7.                 $state .= “<option value='” . $_state->getCode() . “‘>” . $_state->getDefaultName() . “</option>”;
  8.             }
  9.         }
  10.         echo $state;
  11.     }

We also contribute great content of Magento Download for FREE (all versions)

DON'T MISS THE CHANCE!

magento download

About Champ

Leave a Reply

Your email address will not be published. Required fields are marked *

Heap | Mobile and Web Analytics