首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Magento : Region API名称= null

Magento : Region API名称= null
EN

Stack Overflow用户
提问于 2011-12-14 04:36:47
回答 2查看 1.1K关注 0票数 1

在Magento 1.5中,我调用了region API。它在我的开发环境中工作得很好,并返回以下内容:

代码语言:javascript
复制
[{"region_id":"66","code":"AB","name":"Alberta"},
{"region_id":"67","code":"BC","name":"British Columbia"},
{"region_id":"68","code":"MB","name":"Manitoba"},
{"region_id":"69","code":"NL","name":"Newfoundland and Labrador"},
{"region_id":"70","code":"NB","name":"New Brunswick"},
{"region_id":"71","code":"NS","name":"Nova Scotia"},
{"region_id":"72","code":"NT","name":"Northwest Territories"},
{"region_id":"73","code":"NU","name":"Nunavut"},
{"region_id":"74","code":"ON","name":"Ontario"},
{"region_id":"75","code":"PE","name":"Prince Edward Island"},
{"region_id":"76","code":"QC","name":"Quebec"},
{"region_id":"77","code":"SK","name":"Saskatchewan"},
{"region_id":"78","code":"YT","name":"Yukon Territory"}]

然后,当在我的暂存环境中调用此API时,结果是相同的,只是每个单一名称都是空的,即使id和代码都很好:

代码语言:javascript
复制
[{"region_id":"66","code":"AB","name":null},
{"region_id":"67","code":"BC","name":null},
{"region_id":"68","code":"MB","name":null},
{"region_id":"69","code":"NL","name":null},
{"region_id":"70","code":"NB","name":null},
{"region_id":"71","code":"NS","name":null},
{"region_id":"72","code":"NT","name":null},
{"region_id":"73","code":"NU","name":null},
{"region_id":"74","code":"ON","name":null},
{"region_id":"75","code":"PE","name":null},
{"region_id":"76","code":"QC","name":null},
{"region_id":"77","code":"SK","name":null},
{"region_id":"78","code":"YT","name":null}]

你知道是什么导致了这一切吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-24 03:42:32

我不知道,你有没有找到解决方案,但我也有同样的问题。这似乎是magento核心代码中的一个bug (详见region-api)。很明显,这个bug一直存在到3年或更长时间...然而,我找到了解决这个问题的方法:

转到Mage_Directory_Model_Region_Api (在Magento 1.6.2中,此文件位于app/code/core/Mage/Directory/Model/Region/Api.php)中),并更改以下行:

代码语言:javascript
复制
class Mage_Directory_Model_Region_Api extends Mage_Api_Model_Resource_Abstract
{
    /**
    * Retrieve regions list
    *
    * @param string $country
    * @return array
    */
    public function items($country)
    {
        try {
            $country = Mage::getModel(’directory/country’)->loadByCode($country);
        } catch (Mage_Core_Exception $e) {
            $this->_fault(’country_not_exists’, $e->getMessage());
        }

        if (!$country->getId()) {
            $this->_fault(’country_not_exists’);
        }

        $result = array();
        foreach ($country->getRegions() as $region) {
            $region->setName($region->getName());  // This is the important line to set the name
            $result[] = $region->toArray(array(’region_id’, ‘code’, ‘name’));
        }

        return $result;
    }
} 
票数 5
EN

Stack Overflow用户

发布于 2014-12-17 23:31:03

我建议不要修改核心代码!修复是有效的,但最好是重写文件,或者至少将其复制到本地。

向config.xml添加重写:

代码语言:javascript
复制
<config>
    <modules>
        <Module_Namespace>
            <version>0.1.0</version>
        </Module_Namespace>
    </modules>
    <global>
        <models>
            <directory>
                <rewrite>
                    <region_api>Module_Namespace_Model_Region_Api_Api</region_api>
                </rewrite>
            </directory>
        </models>
    </global>
</config>

将Mage_Directory_Model_Region_Api复制到Module_Namespace_Model_Region_Api_Api并应用第56行中的修复:

代码语言:javascript
复制
class Module_Namespace_Model_Region_Api_Api extends Mage_Api_Model_Resource_Abstract
{
    /**
     * Retrieve regions list
     *
     * @param string $country
     * @return array
     */
    public function items($country)
    {
        try {
            $country = Mage::getModel('directory/country')->loadByCode($country);
        } catch (Mage_Core_Exception $e) {
            $this->_fault('country_not_exists', $e->getMessage());
        }

        if (!$country->getId()) {
            $this->_fault('country_not_exists');
        }

        $result = array();
        foreach ($country->getRegions() as $region) {
            $region->setName($region->getName()); //Fix
            $result[] = $region->toArray(array('region_id', 'code', 'name'));
        }

        return $result;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8495840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档