Tuesday, March 27, 2012

Magento show subcateogry instead of Products in grid/list Page

Magneto Have default functionality of showing the Products in list view or Grid view
But some time we require to show the Sub catgeory in list page and if that sub catrgoy have another subcategory

For showing that we need to work on following



1) Copy the Category.php file from Mage/Catalog/Model/Resource core to local.

Create directories required under app/code/local/age/Catalog/Model/Resource.

2) 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')  //like we have added over here to show the image for the category
            ->addAttributeToSelect('all_children')
            ->addAttributeToSelect('is_anchor')
            ->addAttributeToFilter('is_active', 1)
            ->addIdFilter($category->getChildren())
            ->setOrder('position', 'ASC')
            ->joinUrlRewrite()
            ->load();
        return $collection;
    }

3) 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();
//print_r($_img);
        ?>

<div class="item">

                   <a href="<?php echo $cur_category->getURL() ?>"><?=$cur_category->getName();?></a>
    <a href="<?php echo $cur_category->getURL() ?>"> <img src="<?php echo $cur_category->getImageUrl() ?>" width="200" height="200"   /></a>
</div>   <br />
<br />
         

                <?php endif?>
    <?php endforeach;?>

</div></div>
<?php
}
else
{
?>
    <?php echo $this->getProductListHtml() ?>
<?php
}
?>

that's it refresh the cache Sub Categroy will show instead of Products

No comments:

Post a Comment

Thankyou for your Comments

LinkWithin

Blog has moved, searching new blog...