vohqvq
Last Updated: May 29, 2017
·
14
· Sushant Thorat

I am gettig all my catagorries and subcatagories in json string,but I want only active catagories in my json string.

<?php
class AbCategoryObserverModelObserver{
public function updateTopMenuJson(Varien
EventObserver $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 = jsonencode($this->getCategoryTree(3));
echo "

";print
r($arr);
echo("<script>console.log('PHP: ".$arr."');</script>");
fileputcontents("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
?>

Say Thanks
Respond