Dali
Dali

Reputation: 7882

Magento: get Categories tree don't show hidden categories

I use this code to show the catagories tree:

$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>'.
        $category->getId().' <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); 

How can I modify it to show also hidden categories recursivly ?

Thanks a lot.

Upvotes: 0

Views: 1294

Answers (1)

erwin_smit
erwin_smit

Reputation: 700

Instead of using the getCategories() method you could set up a custom collection. This will show the hidden categories, unless told otherwise.

For example:

$categories = Mage::getModel('catalog/category')
->load(Mage::app()->getStore()->getRootCategoryId())
->getCollection()
->addAttributeToSort('position', 'ASC')
->addFieldToFilter('parent_id',Mage::app()->getStore()->getRootCategoryId())
->addFieldToFilter('include_in_menu',1)
->addAttributeToSelect('name')

Upvotes: 3

Related Questions