【发布时间】:2011-03-29 18:50:48
【问题描述】:
如何以编程方式从 2 字符状态缩写中获取 Magento 中的 region_id?如果这很重要,我正在使用 Magento 1.4.2。
【问题讨论】:
标签: php magento e-commerce
如何以编程方式从 2 字符状态缩写中获取 Magento 中的 region_id?如果这很重要,我正在使用 Magento 1.4.2。
【问题讨论】:
标签: php magento e-commerce
$regionModel = Mage::getModel('directory/region')->loadByCode($regionCode, $countryCode);
$regionId = $regionModel->getId();
【讨论】:
获取与特定国家/地区相关的所有州/地区的集合。
/**
* 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>
【讨论】:
这对我有用。
<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>
【讨论】: