在magento注册中获取所有美国州的州下拉列表

时间:2013-03-18 14:18:36

标签: magento registration state country

我希望你们都做得很好,

我在magento 1.6.0.1工作。 我目前的网站仅供美国使用,因此在注册页面中不需要国家下拉列表,并且必须在状态下拉列表中显示所有美国州。

现在我在隐藏字段中设置国家/地区值“US”。

那么我如何才能获得所有美国州的州下拉列表

3 个答案:

答案 0 :(得分:2)

在register.phtml中添加此项以显示美国的所有州:

                <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->__('State/Province') ?>" class="validate-select">
                            <option value=""><?php echo $this->__('Please select region, state or province') ?></option>
                    <?php
                    $this->setData('country_id','US'); // or 'FR'..., default is 'US'
                    $regions     =   $this->getRegionCollection();
                    foreach($regions as $region)
                    {
                        echo "<option value=$region[name]>".$region['name'] . "</option>";
                    }
                    ?>

                        </select>
                        <script type="text/javascript">
                        //<![CDATA[
                            $('region_id').setAttribute('defaultValue', "<?php echo $this->getFormData()->getRegionId() ?>");
                        //]]>
                        </script>

                        <input type="text" id="region" name="region" value="<?php echo $this->escapeHtml($this->getRegion()) ?>" title="<?php echo $this->__('State/Province') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('region') ?>" style="display:none;"   />
                    </div>
                </div>
            </li>
            <li class="fields">
                  <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>

并将底部javascript替换为:

  <script type="text/javascript">
        var dataForm = new VarienForm('form-validate', true);
        new RegionUpdater('country', 'region', 'region_id', <?php echo $this->helper('directory')->getRegionJson() ?>);
    </script>

希望它会有所帮助

答案 1 :(得分:0)

来自Country and States in Magento

 <script type="text/javascript">
        var dataForm = new VarienForm('form-validate', true);
        new RegionUpdater('country', 'region', 'region_id', <?php echo $this->helper('directory')->getRegionJson() ?>);
    </script>

答案 2 :(得分:0)

这对我有用。

    <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->__('State/Province') ?>" class="validate-select">
                                <option value=""><?php echo $this->__('Please select region, state or province') ?></option>
                            <?php                        
                            $regions = Mage::getModel('directory/country')->load('US')->getRegions();
                            foreach($regions as $region)
                            {
                                echo "<option value=$region[name]>".$region['name'] . "</option>";
                            }
                            ?>

                        </select>                       

                        </div>
</div>