在Magento 1.5中,我调用了region 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时,结果是相同的,只是每个单一名称都是空的,即使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}]你知道是什么导致了这一切吗?
发布于 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)中),并更改以下行:
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;
}
} 发布于 2014-12-17 23:31:03
我建议不要修改核心代码!修复是有效的,但最好是重写文件,或者至少将其复制到本地。
向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;
}
}https://stackoverflow.com/questions/8495840
复制相似问题