获取region_id来自州的缩写 - Magento 1.4.2

时间:2011-03-29 18:50:48

标签: php magento e-commerce

如何以编程方式从2个字符的州名缩写中获取Magento中的region_id?如果重要的话,我正在使用Magento 1.4.2。

3 个答案:

答案 0 :(得分:26)

$regionModel = Mage::getModel('directory/region')->loadByCode($regionCode, $countryCode);
$regionId = $regionModel->getId();

答案 1 :(得分:1)

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

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

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