Skip to content

Instantly share code, notes, and snippets.

@haruair
Last active May 6, 2018 22:28
Show Gist options
  • Save haruair/b8ff4fb128488f5e1dbf to your computer and use it in GitHub Desktop.
Save haruair/b8ff4fb128488f5e1dbf 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
}
?>
@ProxiBlue
Copy link

@ink-ru: Make sure you set a value for $recursionLevel variable used on that line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment