Skip to content

Instantly share code, notes, and snippets.

@sonnygauran
Forked from wilhelm-murdoch/getChildCategories.php
Last active August 29, 2015 14:06
Show Gist options
  • Save sonnygauran/9ecacd87d62be47d9a63 to your computer and use it in GitHub Desktop.
Save sonnygauran/9ecacd87d62be47d9a63 to your computer and use it in GitHub Desktop.
Recursively get Magento categories and children. Parameter to return only ID, and active only.
function getCategories(Mage_Catalog_Model_Category $ParentCategory, $id_only = false, $active = true) {
$return = array();
foreach(explode(',', $ParentCategory->getChildren()) as $categoryId) {
$Category = Mage::getModel('catalog/category')->load($categoryId);
if ($Category->getIsActive()) {
if ($id_only) {
$return[] = $categoryId;
} else {
$return[$categoryId] = array(
'id' => $Category->getId(),
'name' => $Category->getName(),
'slug' => $Category->getUrlKey(),
'url' => $Category->getUrl(),
'active' => $Category->getIsActive(),
'children' => array()
);
}
}
if($Category->getChildren()) {
if ($id_only) {
$return = array_merge($return, $this->getCategories($Category, $id_only, $active));
} else {
$return[$categoryId]['children'] = $this->getCategories($Category, $id_only, $active);
}
}
}
return $return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment