Magento – Fetch catalogCategoryTree through Soap API fails with custom attribute

apicategorysoap

For a Magento 1.7.0.2 instance, I'm trying to integrate a custom attribute legacy_id in the category listing. This will be used to match categories from a backend system with the Magento categories.

With this in place, inserting a new category, or updating an existing one, works perfectly using the API (catalog_category.create/update). Retrieving a single category also works perfectly, as the legacy_id property works just great on all CRUD operations.

However, what does not work is retrieving the category tree through the SOAP v2, with a call like this:

result = $proxy->catalogCategoryTree($session, 'category_root_id');

This will result in the following exception:

SOAP-ERROR: Encoding: object has no 'legacy_id' property in /var/www/vhosts/MAGENTO/docs/includes/src/Zend_Soap_Server.php on line 832

The question is: what can be the cause of this, e.g. catalog_category working but category_tree does not?

Or perhaps someone has some tips as on how I can produce more descriptive error messages for debugging?

Best Answer

The tree method returns objects of type catalogCategoryTree that looks like this:

<complexType name="catalogCategoryTree">
    <all>
        <element name="category_id" type="xsd:int"/>
        <element name="parent_id" type="xsd:int"/>
        <element name="name" type="xsd:string"/>
        <element name="position" type="xsd:int"/>
        <element name="level" type="xsd:int"/>
        <element name="children" type="typens:ArrayOfCatalogCategoryEntities"/>
    </all>
</complexType>

This one does not contain the field legacy_id. You can extend the api and add it. Here are some pointers on who to extend the wsdl.

Also take a look at the tree method in the category API, specially the last lines:

$collection = Mage::getModel('catalog/category')->getCollection()
            ->setStoreId($this->_getStoreId($store))
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('is_active');

$tree->addCollectionData($collection, true);
return $this->_nodeToArray($root);

Your attribute is not added to the collection. So you can override this method to add it.
Also the returned value is processed by the method _nodeToArray() that looks like this:

protected function _nodeToArray(Varien_Data_Tree_Node $node)
{
    // Only basic category data
    $result = array();
    $result['category_id'] = $node->getId();
    $result['parent_id']   = $node->getParentId();
    $result['name']        = $node->getName();
    $result['is_active']   = $node->getIsActive();
    $result['position']    = $node->getPosition();
    $result['level']       = $node->getLevel();
    $result['children']    = array();

    foreach ($node->getChildren() as $child) {
        $result['children'][] = $this->_nodeToArray($child);
    }

    return $result;
}

You may need to override this also to include your attribute value in the result array.

Related Topic