Listing regions from a country code

本文介绍了一种在Magento中获取特定国家所有地区的有效方法。通过使用Magento的目录区域模型,开发者可以轻松地根据国家ID筛选地区集合,这对于填充地址选择页面或运输税收页面的下拉菜单非常有用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Once and a while you may want to get a collection of the regions that are associated with a country in Magento. For example, the United States is of course a collection of… states. In Magento these are referred to as regions (Japan’s prefectures would hence be referred to as regions as well.)

 

So for a given country (and that country’s ID or country code) you might want to find all the regions in that country. Which are usually used to populate a dropdown menu on the address selection pages or shipping tax pages.

 

$collection = Mage::getModel('directory/region')->getResourceCollection()
->addCountryFilter($this->getCountryId())
->load();

 It’s great when ORM works for a developer. The problem with Magento I find is the complete lack of documentation. I only discovered the above technique after 20 minutes of grepping the app/code/core directory…

foreach($collection as $region) {
    Mage::log(print_r($region, true));
}

 Note some of the more useful attributes of that $region object are:

$region->getData('default_name')
$region->getData('code')

Which give responses along the lines of South Australia and SA, respectively.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值