Skip to content

Instantly share code, notes, and snippets.

@rbk
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbk/d4c2db870fd404546331 to your computer and use it in GitHub Desktop.
Save rbk/d4c2db870fd404546331 to your computer and use it in GitHub Desktop.
Get category list magento
<?php
// Credit: http://excellencemagentoblog.com/blog/2011/09/14/magento-generate-category-tree-recursively/
$rootcatId= Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCategories($rootcatId);
function get_categories($categories) {
$array= '<ul>';
foreach($categories as $category) {
$cat = Mage::getModel('catalog/category')->load($category->getId());
$count = $cat->getProductCount();
$array .= '<li>'.
'<a href="' . Mage::getUrl($cat->getUrlPath()). '">' .
$category->getName() . "(".$count.")</a>\n";
if($category->hasChildren()) {
$children = Mage::getModel('catalog/category')->getCategories($category->getId());
$array .= get_categories($children);
}
$array .= '</li>';
}
return $array . '</ul>';
}
echo get_categories($categories);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment