2

as

<?php
class Ahybemeir_CategoryObserver_Model_Observer{
  public function updateTopMenuJson(Varien_Event_Observer $observer){
      $_helper = Mage::helper('catalog/category');
      $_categories = $_helper->getStoreCategories();
      $recursionLevel = 3; // Add recursion level
      $storeId = 1; // Add your Store ID for which you want to get category tree
      $node = '';
      $categoryTreeData = $this->getCategoryTree($recursionLevel, $storeId);
      $arr = json_encode($this->getCategoryTree(3));
      echo "<pre>";print_r($arr);
  echo("<script>console.log('PHP: ".$arr."');</script>");
  file_put_contents("cat_node.json", $arr);
}

protected function getCategoryTree($recursionLevel, $storeId = 1)
{
    $parent = Mage::app()->getStore($storeId)->getRootCategoryId();
    $tree = Mage::getResourceModel('catalog/category_tree');
    /* @var $tree Mage_Catalog_Model_Resource_Category_Tree */

    $nodes = $tree->loadNode($parent)
        ->loadChildren($recursionLevel)
        ->getChildren();
    $collection = $tree->getCollection()
        ->addAttributeToSelect('description');
    $tree->addCollectionData($collection, false, $parent);

    $categoryTreeData = array();
    foreach ($nodes as $node) {
        if ($node->getIsActive()) {
            $categoryTreeData[$node->getData('entity_id')] = $this->getNodeChildrenData($node);
        }
    }

    return $categoryTreeData;
}

protected function getNodeChildrenData(Varien_Data_Tree_Node $node)
{
    $categoryData = array(
      'title' => $node->getData('name'),
      'url'   => $node->getData('url_key'),
      'id'    => $node->getId(),
      'description' => $node->getData('description'),
      'img' => $node->getImageUrl(),
      // 'mega_menu1'=> $node->getData('mega_menu')
      'mega_menu'=> 'false'
    );

    foreach ($node->getChildren() as $childNode) {
      if (!array_key_exists('children', $categoryData)) {
          $categoryData['mega_menu'] = 'true';
          $categoryData['children'] = array();
      }

        $categoryData['children'][$childNode->getData('entity_id')] = $this->getNodeChildrenData($childNode);
    }
    return $categoryData;
}
} // End of Class
?>
1
  • I want o get image data in my json tree. so that i will use it on frontend Commented Jun 8, 2017 at 7:36

1 Answer 1

2

To add category images change

$collection = $tree->getCollection()
    ->addAttributeToSelect('description');
$tree->addCollectionData($collection, false, $parent);

To:

$collection = $tree->getCollection()
    ->addAttributeToSelect(array('description', 'image'));
$tree->addCollectionData($collection, false, $parent);

Edit:

For the category images are store in media/catalog/category folder, so you can use this:

$imageUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/category/' . $node->getImage();
3
  • Thank you..but now i get only image name..noy getting full url, so that it will display on frontend.Now its just displaying a curupted image. Commented Jun 8, 2017 at 10:02
  • Please help me for getting the image url,so that i will display it on frontend. Commented Jun 8, 2017 at 10:03
  • Updated answer.
    – sv3n
    Commented Jun 8, 2017 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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