Thursday, 7 June 2012

Display Categories and SubCategories in Magento

First Put Xml in catalog.xml

 <reference name="left">
<block type="catalog/navigation" name="catalog.category" template="catalog/category/list_category.phtml"/>
 </reference>

and make a file list_category.phtml. path is->

app\design\frontend\base\default\template\catalog\category\list_category.phtml
and put below given code.
           

 Display Top Level Categories Only 

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?>
    <ul>
        <?php foreach($_categories as $_category): ?>
            <li>
                <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                    <?php echo $_category->getName() ?>
                </a>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>
*******************************************************************************

Display Top Level Categories and ALL Subcategories 

<?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; ?>

Display Top Level Categories and Current Categories SubCategories

<?php foreach ($this->getStoreCategories() as $_category): ?>
<li>
<a href="<?php echo $this->getCategoryUrl($_category) ?>"><?php echo $this->htmlEscape($_category->getName()) ?></a>
<?php
$_catid=$_category->getId();
$category = Mage::getModel('catalog/category')->load($_catid);
$subcategory = $category->getAllChildren(true);
array_shift($subcategory);
if($subcategory!=null)
{?>
<ul>
<?php
foreach ($subcategory as $sub)
{
$sub1 = Mage::getModel('catalog/category')->load( $sub);
?>
<li><a href="<?php echo $sub1->getUrl();?>"><span>
<?php echo $sub1->getName(); ?>
</span></a></li>
<?php } ?>

</ul>
<?php }?>

</li>
<?php endforeach ?> 

 

2 comments:

  1. Hi please how can I display all subcategories in a subcategory page like for example http://shopaholicng.com/.../accessories-created-desc/belts please mail me on kingsley.udenewu@hotmail.com

    ReplyDelete
  2. I do agree with all the ideas you have presented in your post. They’re really convincing and will certainly work. Still, the posts are very short for newbies. Could you please extend them a little from next time?..Keep this great work
    magento development company in bangalore 

    ReplyDelete

Note: only a member of this blog may post a comment.