Magento – Country dependent State/Province list in a custom module

country-regionscustommagento-1.9magento-enterprisemodule

I am new to magento and stuck on this problem, I am working on module in which I required a state drop down dependent on country, like one page checkout page. I have the country drop down. Can any one help me to show country dependent state drop down in my custom module.

Best Answer

  • First,your block class should extend Block class Mage_Directory_Block_Data. bcoz of this class generate country dropdown for using function getCountryHtmlSelect()

Then add region and country dropdown using below code at your phtml file

  <li class="fields">
                    <div class="field">
                        <label for="region_id" class="required"><em>*</em><?php echo $this->__('State/Province') ?></label>
                        <div class="input-box">
                            <select id="region_id" name="region_id" title="<?php echo $this->quoteEscape($this->__('State/Province')) ?>" class="validate-select" style="display:none;">
                                <option value=""><?php echo $this->__('Please select region, state or province') ?></option>
                            </select>
                            <script type="text/javascript">
                            //<![CDATA[
                                $('region_id').setAttribute('defaultValue', "");
                            //]]>
                            </script>
                            <input type="text" id="region" name="region" value="" title="<?php echo $this->quoteEscape($this->__('State/Province')) ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('region') ?>" style="display:none;" />
                        </div>
                    <div class="field">
                        <label for="country" class="required"><em>*</em><?php echo $this->__('Country') ?></label>
                        <div class="input-box">
                            <?php echo $this->getCountryHtmlSelect() ?>
                        </div>
                    </div>
                </li>
  • Now,you need add some javascript code which will change region dropdown using below code:

new RegionUpdater('country', 'region', 'region_id', <?php echo $this->helper('directory')->getRegionJson() ?>, undefined, 'zip');

Related Topic