Magento:Region API名称= null

时间:2011-12-13 20:36:47

标签: api magento

在Magento 1.5中,我正在调用区域API。它在我的开发环境中工作正常并返回:

[{"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时,结果是相同的,除非每个单独的名称都为null,即使id和代码是好的:

[{"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}]

对于会导致这种情况的任何想法?

2 个答案:

答案 0 :(得分:5)

我不知道,如果你找到了解决方案,但我遇到了同样的问题。它似乎是magento核心代码中的一个错误(详见region-api)。显然这个bug存在到3年或更长时间...... 然而,我发现这是解决它的问题:

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

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

答案 1 :(得分:1)

我建议不要修改核心代码!修复工作正常,但最好重写文件或至少将其复制到本地。

将重写添加到config.xml:

<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行应用修复:

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