Display sub categories on main category page+Magento

Home / Magento / Display sub categories on main category page+Magento

display sub-categories instead of products on main category page+magento

If you want to display Sub Categories list on main category or parent category in magento.
So this artical may be help ful to you for design and

display subcategories on parent category page+magento

.

=> Copy the Category.php file from core to local. Create any directories required under app/code/local. (This is so images can be displayed)

$ cp app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Category.php app/code/local/Mage/Catalog/Model/Resource/Eav/Mysql4/Category.php

=> Add -addAttrbuteToSelect(‘image’) to getChildrenCategories function

    public function getChildrenCategories($category)
    {
        $collection = $category->getCollection();
        /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
        $collection->addAttributeToSelect('url_key')
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('image')
            ->addAttributeToSelect('all_children')
            ->addAttributeToSelect('is_anchor')
            ->addAttributeToFilter('is_active', 1)
            ->addIdFilter($category->getChildren())
            ->setOrder('position', 'ASC')
            ->joinUrlRewrite()
            ->load();
        return $collection;
    }

=> Add or modify the category view template app/design/frontend/yourdiractory_path/default/template/catalog/category/view.phtml

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

$rootcat = $_category->entity_id;
$cat = Mage::getModel('catalog/category')->load($rootcat);
$child = $cat->getChildren();

if($child)
{
$helper     = Mage::helper('catalog/category');
$_helper    = $this->helper('catalog/output');
?>
<div class="category-custom-products">
        <span class="page-title">
        <?php echo $_helper->categoryAttribute($_category, $_category->getName(), 'name') ?></span>

<div class="products-grid">

    <?php foreach ($collection as $cat):?>
        <?php if($_category->getIsActive()):?>
        <?php 
            $cur_category = Mage::getModel('catalog/category')->load($cat->getId());
            //echo $cat->getId();
            $_img = $cur_category->getImageUrl();  
        ?>

<div class="item">
                   <a href="<?php echo $cur_category['url_path']; ?>"><?=$cur_category->getName();?></a>
</div>                
                
                <?php endif?>
    <?php endforeach;?>
    
</div></div>
<?php
}
else
{
?>
    <?php echo $this->getProductListHtml() ?>
<?php
}
?>

With the help of this artical you can get easily all child category and images based on current category.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *