John Brown
John Brown

Reputation: 39

Magento: display all level2 categories

I have categories with 4 levels.
Example :
level0category --> level1category --> level2category -->lastlevel_level3cateogory
is it possible on lastlevel to display the categories that are in level2 and only? (on a block that i load a custom left.phtml)
I was thinking something with automatic category detection and not have to set cat_id=

Edit: this is what i was looking for, after mix of code from different sources.

<?php
    $currentCat = Mage::registry('current_category');

    if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
    {
        // current category is a toplevel category
        $loadCategory = $currentCat;
    }
    else
    {
        // current category is a sub-(or subsub-, etc...)category of a toplevel category
        // load the parent category of the current category
        $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
        // @TODO enhance for more nested category levels to display sub-categories
    }    
    $subCategories = explode(',', $loadCategory->getChildren());

    foreach ( $subCategories as $subCategoryId )
    {
        $cat = Mage::getModel('catalog/category')->load($subCategoryId);


if ($cat->getIsActive())
{
    if ($currentCat->getEntityId() == $subCategoryId)
    {
        echo '<li style="display:none;">'.$cat->getName().'</li>';
    }
    else
    {


  echo '<a href="'.$cat->getURL().'">'.$cat->getName().'</a> <br>';    }
}

    }
?>

Upvotes: 1

Views: 3845

Answers (1)

Oscprofessionals
Oscprofessionals

Reputation: 2174

$categories = Mage::getModel('catalog/category')
                    ->getCollection()
                    ->addAttributeToSelect('*')
                    ->addIsActiveFilter()
                    ->addAttributeToFilter('level',2)
                    ->addOrderField('name');

make sure this line is there

apply ->addAttributeToFilter('level',2)

Upvotes: 1

Related Questions