Jan 5, 2014
kalpesh

Magento display categories and sub-categories

Magento display categories and sub-categories. Below code will show all the parent and child categories along with show/hide functionalities by jQuery.

<div class="block block-layered-nav">
    <div class="block-title">
        <strong><span><?php echo $this->__('Shop By Category') ?></span></strong>
    </div>

    <div class="block-content">
        <?php $productid = Mage::registry('current_product')->getId();
        $product = Mage::getSingleton('catalog/product')->load($productid);
        $parentIds = $product->getCategoryIds();
        $parentId = $parentIds[0];
        $_categories = Mage::getBlockSingleton('catalog/navigation');
        foreach ($_categories->getStoreCategories() as $_category) {
                $category = Mage::getModel('catalog/category');
                $category->load($_category->getId());
                $subcategories = explode(',', $category->getChildren());
                if(!in_array($_category->getId(),$parentIds)) { $hide="display:none"; $inactive="inactive"; } else { $hide=""; $inactive="active"; }
                ?>
                <div style="padding:5px 5px 0px 10px">
                        <div class="cat parent <?php echo $inactive;?>" style="font-size:15px"><?php echo $_category->getName() ?></div>
                        <div class="child" style="<?php echo $hide;?>">
                         <?php foreach ($subcategories as $subcategoryId) {
                                $category->load($subcategoryId);
                                if($category->getChildren() == '') {
                                        if(in_array($subcategoryId,$parentIds)) { $bold = "font-weight:bold"; } else { $bold = ""; }
                                      echo '<div class="subcat" style="'.$bold.'"><a href="' . $category->getURL() . '">' . $category->getName() . '</a></div>';
                                 } else {?>
                                <div class="subcat">
                                        <div class="parent active"><?php echo $category->getName() ?></div>
                                        <div class="child">
                                        <?php $subsubcategories = explode(',', $category->getChildren());
                                        foreach($subsubcategories as $subsubcatid) {
                                                if(in_array($subsubcatid, $parentIds)) { $bold = "font-weight:bold"; } else { $bold = ""; }
                                                $category->load($subsubcatid);
                                                echo '<div style="padding-left:10px;'.$bold.'"><a href="' . $category->getURL() . '">' . $category->getName() . '</a></div>';
                                        } ?>
                                        </div>
                                </div>
                        <?php }
                        }
                        ?>
                        </div>
                </div>
                <?php

        } ?>
    </div>
</div>

<script type="text/javascript">
    jQuery('.block-content .cat').click(function(){
        var t = jQuery(this);
        if(jQuery(this).next().css('display')=='none') {
                jQuery('.col-left .block-content .child').hide();
                t.next().show(); t.next().find('div.child').show("slow");
        } else {
                t.next().hide(); t.next().find('div.child').hide("slow");
        }
    });
</script>

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions