Display sub categories 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 helpful.

first open this file
   app\code\core\Mage\Catalog\Block\Category\View.php
and add this function ( copy and past )


public function getAllSubcategory() {
   $_category = $this->getCurrentCategory();
   $rootcat = $_category->entity_id;
   $collection = Mage::getModel('catalog/category')->getCategories($rootcat);
   $sub_category = Mage::getModel('catalog/category')->load($rootcat);
   $child = $sub_category->getChildren();
   return $child;
}


next open this file
   app\design\frontend\default\yourtemplate\template\catalog\category\View.phtml
and add this code ( copy and past )


<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php
/**
 * Category view template
 *
 * @see Mage_Catalog_Block_Category_View
 */
?>
<?php
$_category = $this->getCurrentCategory();
$helper = Mage::helper('catalog/category');
$_helper = $this->helper('catalog/output');
?>
<?php
if($child = $this->getAllSubcategory())
{
?>
<div class="category-head">
    <h2><?php echo $_category->getName(); ?></h2>
</div>
<div class="listing-catalogimg-grid catalogimg-listing">
    <ul class="grid-row">
<?php
        $child = $this->getAllSubcategory();
        $children=explode(",", $child);
        foreach($children as $_category):
        $collection= Mage::getModel('catalog/category')->load($_category);
        ?>
        <li class="item">
            <div class="category-entry">
                <h5>
                    <a class="easycategoriesroot" title="<?php echo $collection->getName(); ?>" href="<?php echo $helper->getCategoryUrl($collection);?>"><?php echo $collection->getName(); ?></a>
                </h5>
                <a class="product-image" href="<?php echo $helper->getCategoryUrl($collection);?>">
                    <img  width="158" height="157" src="<?php echo Mage::getBaseUrl('media').'catalog/category/'.$collection->getThumbnail()  ?>" />
                </a>
                <a class="view_all_btn" href="<?php echo $helper->getCategoryUrl($collection);?>"><?php echo $this->__('View All') ?></a>
            </div>
        </li>
        <?php endforeach;?>
    </ul>
</div>
<?php }else{ ?>
<?php echo $this->getProductListHtml() ?>
<?php } ?>


refresh Your browser
With the help of this artical you can get easily all child category and images based on current category. :)


::: Another way :::

open this file
   app\design\frontend\default\yourtemplate\template\catalog\category\View.phtml
and add this code ( copy and past ) replace all code in view.phtml

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php
/**
 * Category view template
 *
 * @see Mage_Catalog_Block_Category_View
 */
?>
<?php
$_category = $this->getCurrentCategory();
$helper = Mage::helper('catalog/category');
$_helper = $this->helper('catalog/output');
?>
<?php
$_category = $this->getCurrentCategory();
$rootcat = $_category->entity_id;
$collection = Mage::getModel('catalog/category')->getCategories($rootcat);
$sub_category = Mage::getModel('catalog/category')->load($rootcat);
$child = $sub_category->getChildren();
$children= json_decode($child);
$children=explode(",", $child);
$count=count($children);
$_collectionSize = $count;
$_columnCount = 4;
if($child)
{
?>
<div class="category-head">
    <h2><?php echo $_category->getName(); ?></h2>
</div>
<?php
$_category  = $this->getCurrentCategory();
    $_imgHtml   = '';
    if ($_imgUrl = $_category->getImageUrl()) {
        $_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></p>';
        $_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
    }
?>
<?php if($_imgUrl): ?>
    <?php echo $_imgHtml ?>
<?php endif; ?>
<?php if($_description=$this->getCurrentCategory()->getDescription()): ?>
    <div class="category-description std">
        <?php echo $_helper->categoryAttribute($_category, $_description, 'description') ?>
    </div>
<?php endif; ?>
<?php if($this->isContentMode()): ?>
    <?php echo $this->getCmsBlockHtml() ?>
<?php elseif($this->isMixedMode()): ?>
    <?php echo $this->getCmsBlockHtml() ?>
        <div class="category-products">
            <?php
            $i=0;
            foreach($children as $_category):
            if ($i++%$_columnCount==0):
            ?>
            <ul class="products-grid">
            <?php
            endif;
            $collection= Mage::getModel('catalog/category')->load($_category);
            ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <a class="product-image" href="<?php echo $helper->getCategoryUrl($collection);?>">
                    <img width="160" height="130" src="<?php echo Mage::getBaseUrl('media').'catalog/category/'.$collection->getThumbnail()  ?>" />
                </a>
                <div class="cont">
                    <a class="easycategoriesroot" title="<?php echo $collection->getName(); ?>" href="<?php echo $helper->getCategoryUrl($collection);?>">
                        <?php echo $collection->getName(); ?>
                    </a>
                </div>
            </li>
            <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
            </ul>
            <?php endif; ?>
            <?php endforeach;?>
        </div>
        <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
<?php elseif($this->getProductListHtml()):?>   
        <div class="category-products">
            <?php
            $i=0;
            foreach($children as $_category):
            if ($i++%$_columnCount==0):
            ?>
            <ul class="products-grid">
            <?php
            endif;
            $collection= Mage::getModel('catalog/category')->load($_category);
            ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <a class="product-image" href="<?php echo $helper->getCategoryUrl($collection);?>">
                    <img width="160" height="130" src="<?php echo Mage::getBaseUrl('media').'catalog/category/'.$collection->getThumbnail()  ?>" />
                </a>
                <div class="cont">
                    <a class="easycategoriesroot" title="<?php echo $collection->getName(); ?>" href="<?php echo $helper->getCategoryUrl($collection);?>">
                        <?php echo $collection->getName(); ?>
                    </a>
                </div>
            </li>
            <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
            </ul>
            <?php endif; ?>
            <?php endforeach;?>
        </div>
        <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
<?php endif;?>
<?php }else{ ?>
    <?php if($this->isContentMode()): ?>
    <?php echo $this->getCmsBlockHtml() ?>

    <?php elseif($this->isMixedMode()): ?>
        <?php echo $this->getCmsBlockHtml() ?>
        <?php echo $this->getProductListHtml() ?>

    <?php else: ?>
        <?php echo $this->getProductListHtml() ?>
    <?php endif; ?>
    <script type="text/javascript">decorateGeneric($$('ul.grid-row'), ['odd','even','first','last'])</script>
<?php } ?>

Enjoy :)

2 comments:

  1. How do I get the main category description to appear above the sub-categories?

    ReplyDelete
  2. Great blog. All posts have something to learn. Your work is very good and i appreciate you and hoping for some more informative posts.keep writing
    magento development company in bangalore 

    ReplyDelete