【问题标题】:Get region_id From State Abbreviation - Magento 1.4.2从州缩写获取 region_id - Magento 1.4.2
【发布时间】:2011-03-29 18:50:48
【问题描述】:

如何以编程方式从 2 字符状态缩写中获取 Magento 中的 region_id?如果这很重要,我正在使用 Magento 1.4.2。

【问题讨论】:

    标签: php magento e-commerce


    【解决方案1】:
    $regionModel = Mage::getModel('directory/region')->loadByCode($regionCode, $countryCode);
    $regionId = $regionModel->getId();
    

    【讨论】:

    • 如果您需要,您可以随时 loadByName!名称类似于“德克萨斯”。
    【解决方案2】:

    获取与特定国家/地区相关的所有州/地区的集合。

    /**
     * Get region collection
     * @param string $countryCode
     * @return array
     */
    public function getRegionCollection($countryCode)
    {
        $regionCollection = Mage::getModel('directory/region_api')->items($countryCode);
        return $regionCollection;
    }
    

    使用区域集合填充区域列表。国家代码(例如 NL、NP、EN)作为参数传递给 getRegionCollection 函数。

    $regionCollection = $this->getRegionCollection($countryCode);
    
    <select name='customer[region]' id='customer:region' class="validate-select" >
        <option>Please select region, state or province</option>
        <?php
            foreach($regionCollection as $region) {
                ?>
                <option value="<?php echo $region['name'] ?>" ><?php echo $region['name'] ?></option>
                <?php
            }
        ?>
    
    </select> 
    

    【讨论】:

      【解决方案3】:

      这对我有用。

      <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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-04
        • 2015-05-07
        • 1970-01-01
        相关资源
        最近更新 更多