1
votes

In Magento 1.5, I'm making a call to the region API. It works fine in my development environment and returns this:

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

Then when this API is called in my staging environment, the result is the same except every singe name is null, even if the id and the code are good:

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

Any idea on what would cause this?

2

2 Answers

5
votes

I don't know, if you found a solution yet, but I just had the same issue. It seems to be a bug in the magento core-code (in detail in the region-api). Obviously the bug exists till 3 years or more... Nevertheless here is what I found to solve it:

Go to Mage_Directory_Model_Region_Api (in Magento 1.6.2, this file is located in app/code/core/Mage/Directory/Model/Region/Api.php) and change the following line below:

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
votes

I recommend not to modify core code! The fix works, but better to rewrite the file or at least copy it to local.

Add rewrite to 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>

Copy Mage_Directory_Model_Region_Api to Module_Namespace_Model_Region_Api_Api and apply the fix in line 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;
    }
}