0

I need load category image for the category tree. Currently I found a method to load category tree. But its not showing image of the category.

    public function getCategoryTree()
    {
        $model = Mage::getModel('catalog/category_api');
        $treeData = $model->tree();
        return $this->_sendJsonRespond('success', $treeData, null, null);
    }
1

You need to overwrite the tree and_nodeToArray functions of Mage_Catalog_Model_Category_Api :

/**
 * Retrieve category tree
 *
 * @param int $parent
 * @param string|int $store
 * @return array
 */
public function tree($parentId = null, $store = null)
{
    if (is_null($parentId) && !is_null($store)) {
        $parentId = Mage::app()->getStore($this->_getStoreId($store))->getRootCategoryId();
    } elseif (is_null($parentId)) {
        $parentId = 1;
    }

    /* @var $tree Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Tree */
    $tree = Mage::getResourceSingleton('catalog/category_tree')
        ->load();

    $root = $tree->getNodeById($parentId);

    if($root && $root->getId() == 1) {
        $root->setName(Mage::helper('catalog')->__('Root'));
    }

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

    $tree->addCollectionData($collection, true);

    return $this->_nodeToArray($root);
}

/**
 * Convert node to array
 *
 * @param Varien_Data_Tree_Node $node
 * @return array
 */
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['image']       = $node->getImage();
    $result['children']    = array();

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

    return $result;
}

Following your needs, I think that retrieving the collection of categories and formating it to your needs would be more advised.

  • what is $node and $parentId – Dushan Oct 23 '15 at 8:21
  • I modified the post to make it fit to these two things. it was bad copy paste... sorry. parentId is the id of the category for wich you want to get the tree. – Christophe Ferreboeuf Oct 23 '15 at 9:04
  • thanks. there's another issue in _nodeToArray function call. can you please check again – Dushan Oct 23 '15 at 9:14
  • I think you better overwrite Mage_Catalog_Model_Category_Api or retrieve collection and format it to your needs. – Christophe Ferreboeuf Oct 23 '15 at 9:18
  • I'm new to magento. I can't understand what you mean – Dushan Oct 23 '15 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.