frshjb373
frshjb373

Reputation: 627

Show all subcategories of current parent caregory in sidebar navigation in MAGENTO

Trying to build sidebar category structure in Magento so that all children for an active category show when clicked. Using below as sample, when you go into main catalog only Main Cats appear. Then when clicking any Sub Cat the children for that respective category appear and so on.

For example

Main Cat 1
    Sub Cat 1
        Sub/Sub 1
        Sub/Sub 1
        Sub/Sub 1
    Sub Cat 1
    Sub Cat 1
Main Cat 2
Main Cat 3

Here's the current code I have, but once you get to the last category, only the Main Cats show (in other words, if you click on Sub/Sub, the menu closes and shows only the Main Cats).

    <aside id="sidebar">
        <div class="sidebar-nav">
            <h2><?php echo $this->__('Products') ?></h2>
<ul>
<?php foreach ($store_cats as $cat) {
    if ($cat->getName() == $current_cat) {
        echo '<li><a href="'.$this->getCategoryUrl($cat).'">'.$cat->getName()."</a><ul>";
        foreach ($obj->getCurrentChildCategories() as $subcat) {
            echo '<li><a href="'.$this->getCategoryUrl($subcat).'">'.$subcat->getName()."</a></li>";
        }
        echo "</ul></li>";
    } else {
        echo '<li><a href="'.$this->getCategoryUrl($cat).'">'.$cat->getName()."</a></li>";
    }
} ?>
</ul>
        </div>
        <div class="sidebar-nav">
             <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('holiday-nav-links')->toHtml() ?> 
        </div>
        <div class="sidebar-nav">
             <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('about-us-nav-links')->toHtml() ?> 
        </div>
    </aside>

Any help is much appreciated. Thank you in advance for the help!

Upvotes: 1

Views: 6335

Answers (2)

anz
anz

Reputation: 1042

Had to do this very thing a few days back . get my whole function, may include some unnecessary html parts though. Shows 2nd level categories level onwards (for 3rd level category view as well)

public function getCatTree()
 {
        $treeHtml = '';
        $_helper = Mage::helper('catalog/category');
        $_categories = $_helper->getStoreCategories();
        $category = Mage::registry('current_category');


        $level = $category->getLevel();
        switch($level)
        {
            case 4 :  
                    $level3Cat = $category->getParentCategory();
                    $level2Cat = $level3Cat->getParentCategory();
                    break;

            case 3 :
                    $level2Cat = $category->getParentCategory();
                    break;

            case 2 :
                    $level2Cat = $category;
                    break;

            default :
                    $level2Cat = null;  
                    break;
        }

        //get the level 2 category ID
        $categoryId = $level2Cat->getId();

        if (count($_categories) > 0) 
        {           
            foreach ($_categories as $_category) 
            {
                //match with the level 2 category, then list all its children
                if ($_category->getId() == $categoryId) 
                {
                    $_category = Mage::getModel('catalog/category')->load($_category->getId());
                    $_catChildrens = $_category->getAllChildren();

                    foreach(explode(',',$_catChildrens) as $index=>$child)
                    {
                            $cat = Mage::getModel('catalog/category')->load($child);
if($cat->getLevel() == 3 && $cat->getIsActive())
                            {
                                $isParent = ($cat->hasChildren()) ? 'parent' : '';
                                $treeHtml .= '<li class="level1 nav'.$cat->getLevel().' '.$isParent.'">'.
                                            '<a href="'.$cat->getUrl().'">'.
                                              $cat->getName().
                                                '</a>';

                                if($cat->hasChildren())
                                {
                                    $treeHtml .= '<div class="sub-menu-wrap"><ul class="level1">';
                                    foreach($cat->getChildrenCategories() as $indx=>$_subcategory)
                                    {
                                        if($_subcategory->getIsActive()) 
                                        {
                                            $cat4 = Mage::getModel('catalog/category')->load($indx);
                                            $treeHtml .= '<li class="level2 nav'.$cat4->getLevel().'">'.
                                            '<a href="'.$cat4->getUrl().'">'.
                                              $cat4->getName().
                                                '</a></li>';

                                        }
                                    }
                                    $treeHtml .= '</ul></div>';
                                }
                                $treeHtml .= '</li>';   
                            }


                    }
                    return $treeHtml;
 }
            }
        }

    }

Upvotes: 1

Shivam
Shivam

Reputation: 2443

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
    <ul>
        <?php foreach($_categories as $_category): ?>
            <li>
                <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                    <?php echo $_category->getName() ?>
                </a>
                <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                <?php $_subcategories = $_category->getChildrenCategories() ?>
                <?php if (count($_subcategories) > 0): ?>
                    <ul>
                        <?php foreach($_subcategories as $_subcategory): ?>
                            <li>
                                <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                    <?php echo $_subcategory->getName() ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

Please check this link http://fishpig.co.uk/magento/tutorials/display-categories-and-subcategories-in-magento/

hope this help you

Upvotes: 2

Related Questions