Skip to content

Instantly share code, notes, and snippets.

@wilhelm-murdoch
Created November 17, 2011 14:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wilhelm-murdoch/1373205 to your computer and use it in GitHub Desktop.
Save wilhelm-murdoch/1373205 to your computer and use it in GitHub Desktop.
Recursive function for Magento that provides useful information regarding a specified category and all children.
<?php
function getCategories(Mage_Catalog_Model_Category $ParentCategory) {
$return = array();
foreach(explode(',', $ParentCategory->getChildren()) as $categoryId) {
$Category = Mage::getModel('catalog/category')->load($categoryId);
$return[$categoryId] = array(
'id' => $Category->getId(),
'name' => $Category->getName(),
'slug' => $Category->getUrlKey(),
'url' => $Category->getUrl(),
'active' => $Category->getIsActive(),
'children' => array()
);
if($Category->getChildren()) {
$return[$categoryId]['children'] = getCategories($Category, $return);
}
}
return $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment