Skip to content

Instantly share code, notes, and snippets.

@eniuz
Forked from haruair/gist:b8ff4fb128488f5e1dbf
Created September 25, 2015 09:54
Show Gist options
  • Save eniuz/7fca76a12c6f5b97601e to your computer and use it in GitHub Desktop.
Save eniuz/7fca76a12c6f5b97601e to your computer and use it in GitHub Desktop.
How to get all category tree node in Magento
<?php
// Usually, you can get category tree from category helper
$helper = Mage::helper('catalog/category');
$nodes = $helper->getStoreCategories();
// return Varien_Data_Tree_Node_Collection
// via Mage_Catalog_Model_Resource_Category
// However, this get method return active category only.
// Most of the samples are for collection of the category.
// This is for a tree node not a normal collection.
// We can get all category tree using the code below:
$parent = Mage::app()->getStore()->getRootCategoryId();
$tree = Mage::getResourceModel('catalog/category_tree');
$nodes = $tree->loadNode($parent)
->loadChildren($recursionLevel)
->getChildren();
$tree->addCollectionData(null, false, $parent, true, false);
// Now, you can use $nodes as category tree
foreach($nodes as $category){
// Do something
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment