1
<?php
class Ab_CategoryObserver_Model_Observer{
  public function updateTopMenuJson(Varien_Event_Observer $observer){
      $_helper = Mage::helper('catalog/category');
      $_categories = $_helper->getStoreCategories();
      $recursionLevel = '';
      $storeId = '';
      $node = '';
      $categoryTreeData = $this->getCategoryTree($recursionLevel, $storeId);
      $categoryData = $this->getNodeChildrenData($node);

      $arr = json_encode(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()->getRootCategoryId();
        $tree = Mage::getResourceModel('catalog/category_tree');
        /* @var $tree Mage_Catalog_Model_Resource_Category_Tree */

        $nodes = $tree->loadNode($parent)
            ->loadChildren($recursionLevel)
            ->getChildren();
        $tree->addCollectionData(null, false, $parent);

        $categoryTreeData = array();
        foreach ($nodes as $node) {
            $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(),
          // '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
?>
0

1 Answer 1

2

The issue seems to be with your function updateTopMenuJson, You have used $node = ''; and then you are passing it to $categoryData = $this->getNodeChildrenData($node); which triggers the error.

Use below code.

<?php
class Ab_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();
        $tree->addCollectionData(null, false, $parent);

        $categoryTreeData = array();
        foreach ($nodes as $node) {
            $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(),
          // '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
?>
3
  • Still got the same error. the function was workinf properly if i wrote both functions inside the public function,but its not good practice to write function inside function..So now i only wants to write a public function and want to call both private function inside this without violating my output.. thanks for the answer, and please help me regarding this as soon as possible. Commented May 29, 2017 at 5:53
  • Check with updated code @SushantThorat. Commented May 29, 2017 at 6:03
  • Yeah!!! Working fine...!! Thnak you so much @Jaimin Sutariya. Commented May 29, 2017 at 6:06

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