Question

How to Extend Category Level in Magento Frontend Site:

Insert this code* under the First Level of the category. In my case, the site's category navbar is located on header.phtml

*The code starts at: Level 2nd Start *improving this code is highly appreciated.

(mikequibin/app/design/frontend/mikequibin/default/template/page/html/header.phtml)

<div class="header-category-menu" style="background: none;">
            <?php $_helper          = Mage::helper('catalog/category') ?>
            <?php $_categories      = $_helper->getStoreCategories() ?>
            <?php $currentCategory  = Mage::getSingleton('catalog/layer')->getCurrentCategory()->getId(); ?>
            <?php if (count($_categories) > 0): ?>
            <div class="menu-bg">
                <ul class="menu clearfix">
                    <?php foreach($_categories as $_category): ?>
                    <?php $class = 'link'; ?>
                    <?php if($_category->getEntityId() == 13 || $_category->getEntityId() == 36): ?>
                    <?php $class = 'link sale'; ?>
                    <?php endif; ?>
                    <?php $class .= $currentCategory==$_category->getEntityId() ? ' active' : NULL; ?>
                    <li class="<?php echo $class; ?> menu-nav" data-id="<?php echo $_category->getId(); ?>">
                        <?php if($_category->getId() != 36) : ?>
                        <i class="icon-caret-down"></i>
                        <?php endif; ?>
                        <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                            <span><?php echo $_category->getName() ?></span>
                        </a>

                        <!-- First Level Start --!>

                        <?php
                            $subCategories = Mage::getModel('catalog/category')->load($_category->getId());
                            $subCategories = $subCategories->getChildrenCategories();
                        ?>
                        <?php if(count($subCategories) != 0): ?>
                        <ul class="submenu">
                        <?php foreach($subCategories as $i => $subCategory): ?>
                            <li>
                                <a href="<?php echo $_helper->getCategoryUrl($subCategory); ?>">
                                <i class="icon-chevron-right"></i><?php echo $subCategory->getName(); ?>

                                    <!-- Second Level Start --!>
                                    <?php if($_category->getIsActive()): ?>
                                        <?php
                                        $subCategories2 = Mage::getModel('catalog/category')->load($subCategory->getId());
                                        $subCategories2 = $subCategories2->getChildrenCategories();
                                        ?>
                                        <?php if(count($subCategories2) != 0): ?>
                                           <?php foreach($subCategories2 as $k => $subCategory2): ?>
                                                <li style="padding-left:20px;">
                                                    <a href="<?php echo $_helper->getCategoryUrl($subCategory2); ?>">
                                                     <?php echo $subCategory2->getName(); ?>

                                                      <!-- Third Level Start --!>
                                                      <?php if($_category->getIsActive()): ?>
                                                         <?php
                                                          $subCategories3 = Mage::getModel('catalog/category')->load($subCategory2->getId());
                                                          $subCategories3 = $subCategories3->getChildrenCategories();
                                                         ?>
                                                            <?php if(count($subCategories3) != 0): ?>
                                                               <?php foreach($subCategories3 as $l => $subCategory3): ?>
                                                                 <li style="padding-left:30px;">
                                                                     <a href="<?php echo $_helper->getCategoryUrl($subCategory3); ?>">
                                                                       <?php echo $subCategory3->getName(); ?>
                                                                     </a>
                                                                 </li>
                                                               <?php endforeach; ?>
                                                           <?php endif; ?>
                                                  <?php endif; ?>
                                                    <!-- Third Level End --!>

                                                    </a>
                                                </li>
                                           <?php endforeach; ?>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                    <!-- Second Level End --!>

                                </a>
                            </li>

                        <?php endforeach; ?>
                        </ul>
                        <?php endif; ?>

                        <!-- First Level End --!>
                    </li>
                    <?php endforeach; ?>
                </ul>
            </div>
            <?php endif; ?>
        </div>
Was it helpful?

Solution

$_helper          = Mage::helper('catalog/category');
$_categories      = $_helper->getStoreCategories();

$html = '';
if (count($_categories) > 0) {
    $html .= '<ul>';
    foreach($_categories as $_category) {
        $html .= '<li>';
            $html .= '<a href="#">'.$_category->getName().'</a>';
            $html .= getRecursiveHtml($_category);
        $html .= '</li>';
    }
    $html .= '</ul>';
}

function getRecursiveHtml($_category,$html='') {
    $_category = Mage::getModel('catalog/category')->load($_category->getId());
    $child = $_category->getChildrenCategories();
    if (count($child) > 0) {
        $html .= '<ul>';
        foreach($child as $_child) {
            $html .= '<li>';
            $html .= '<a href="#">'.$_child->getName().'</a>';
            $html = getRecursiveHtml($_child,$html);
            $html .= '</li>';
        }
        $html .= '</ul>';
    }
    return $html;
}

echo $html;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top