-
-
Save eniuz/7fca76a12c6f5b97601e to your computer and use it in GitHub Desktop.
How to get all category tree node in Magento
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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