Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento: Display sub-category list

I'm building a Magento store and want to be able to display a list of categories and have each category link to its own page.

I have a 'Brands' category with an ID of 42 and I want to display a list of the sub-categories and ensure that each one links to the designated URL key in the CMS.

Has anyone had experience of doing this with Magento?

like image 478
doubleplusgood Avatar asked Nov 29 '22 04:11

doubleplusgood


2 Answers

If you're comfortable editing your theme, this code snippet will bring you a list of all sub-categories of the current category (from the session, so this should work anywhere in your theme). I typically use this in app/design/frontend/default/theme_name/template/catalog/category/view.phtml

<?php
$_category  = $this->getCurrentCategory(); 
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper     = Mage::helper('catalog/category');
?>

<ul>
    <?php foreach ($collection as $cat):?>
            <?php if($_category->getIsActive()):?>
                <?php 
                     $cur_category = Mage::getModel('catalog/category')->load($cat->getId());
                     $_img = $cur_category->getImageUrl();  
                ?>
                <li>
                    <a href="<?php echo $helper->getCategoryUrl($cat);?>">
                         <img src="<?php echo $_img?>" title="<?php echo $cat->getName();?>"/>
                         <cite><?php echo $cat->getName();?></cite>
                    </a>
                </li>
            <?php endif?>
    <?php endforeach;?>
</ul>
like image 137
wookiehangover Avatar answered Nov 30 '22 17:11

wookiehangover


If You want to Display top level categories and subcategories U can do Like This..

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

For Displaying Top Level Categories and Current Categories SubCategories you can Do like ....

<?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 if ($currentCategory && $currentCategory->getId() == $_category->getId()): ?>
                <?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; ?>
            <?php endif; ?>
        </li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>
like image 30
ScoRpion Avatar answered Nov 30 '22 17:11

ScoRpion