Step1: Create IndexController
<?php
class Tbi_Collection_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
}
Step2: Create Layout xml file
<?xml version="1.0"?>
<layout version="0.1.0">
<collection_index_index>
<reference name="content">
<Tbi type="collection/collection" name="product_list" template="collection/collection.phtml">
</block>
</reference>
</collection_index_index>
</layout>
Step3: Block in the local/block/Collection.php
<?php
class Tbi_Collection_Block_Collection extends Mage_Core_Block_Template
{
public function __construct()
{
parent::__construct();
$parent_id = Mage::app()->getStore()->getRootCategoryId();
if($this->getRequest()->getParam('category_id',false)){
$parent_id = $this->getRequest()->getParam('category_id');
}
$collection = Mage::getModel('catalog/category')->getCollection();
$collection->addFieldToFilter('parent_id',$parent_id);
$collection->addIsActiveFilter();
$collection->addNameToResult();
$collection->addUrlRewriteToResult();
//$collection->setLoadProductCount(true);
$this->setCollection($collection);
}
protected function _prepareLayout()
{
parent::_prepareLayout();
$parent_id = Mage::app()->getStore()->getRootCategoryId();
if($this->getRequest()->getParam('category_id',false)){
$parent_id = $this->getRequest()->getParam('category_id');
}
$category = Mage::getModel('catalog/category')->load($parent_id);
if ($headBlock = $this->getLayout()->getBlock('head')) {
if ($title = $category->getMetaTitle()) {
$headBlock->setTitle($title);
}
if ($description = $category->getMetaDescription()) {
$headBlock->setDescription($description);
}
if ($keywords = $category->getMetaKeywords()) {
$headBlock->setKeywords($keywords);
}
}
$this->setTitle($category->getName());
$toolbar = $this->getToolbarBlock();
// called prepare sortable parameters
$collection = $this->getCollection();
// use sortable parameters
if ($orders = $this->getAvailableOrders()) {
$toolbar->setAvailableOrders($orders);
}
if ($sort = $this->getSortBy()) {
$toolbar->setDefaultOrder($sort);
}
if ($dir = $this->getDefaultDirection()) {
$toolbar->setDefaultDirection($dir);
}
$toolbar->setCollection($collection);
$this->setChild('toolbar', $toolbar);
$this->getCollection()->load();
return $this;
}
public function getDefaultDirection(){
return 'asc';
}
public function getAvailableOrders(){
return array('name'=> 'Name','position'=>'Position','children_count'=>'Sub Category Count');
}
public function getSortBy(){
return 'name';
}
public function getToolbarBlock()
{
$block = $this->getLayout()->createBlock('collection/toolbar', microtime());
return $block;
}
public function getMode()
{
return $this->getChild('toolbar')->getCurrentMode();
}
public function getToolbarHtml()
{
return $this->getChildHtml('toolbar');
}
}
Create the block class ‘collection/toolbar’ which is used in the above Class
<?php
class Tbi_Collection_Block_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar{
public function getPagerHtml()
{
$pagerBlock = $this->getLayout()->createBlock('page/html_pager');
if ($pagerBlock instanceof Varien_Object) {
/* @var $pagerBlock Mage_Page_Block_Html_Pager */
$pagerBlock->setAvailableLimit($this->getAvailableLimit());
$pagerBlock->setUseContainer(false)
->setShowPerPage(false)
->setShowAmounts(false)
->setLimitVarName($this->getLimitVarName())
->setPageVarName($this->getPageVarName())
->setLimit($this->getLimit())
->setCollection($this->getCollection());
return $pagerBlock->toHtml();
}
return '';
}
}
Step4: .phtml Files
<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<?php $collection = $this->getCollection(); ?>
<div class="page-title">
<h1><?php echo $this->__($this->getTitle()) ?></h1>
</div>
<?php echo $this->getToolbarHtml(); ?>
<?php if($collection->getSize()): ?>
<?php if($this->getMode()!='grid'): ?>
<!-- List Model -->
<?php $_iterator = 0; ?>
<ol class="products-list" id="products-list">
<?php foreach ($collection as $category):
$category = Mage::getModel('catalog/category')->load($category->getId()); ?>
<li class="item<?php if( ++$_iterator == sizeof($collection) ): ?> last<?php endif; ?>">
<?php // Product Image ?>
<a href="<?php echo $category->getUrl() ?>" title="<?php echo $this->stripTags($category->getName()); ?>" class="product-image"><img src="<?php echo $category->getImageUrl() ?>" width="135" height="135" alt="<?php echo $this->stripTags($category->getName()); ?>" /></a>
<?php // Product description ?>
<div class="product-shop">
<div class="f-fix">
<?php $_productNameStripped = $this->stripTags($category->getName(), null, true); ?>
<h2 class="product-name"><a href="<?php echo $category->getUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $category->getName(); ?></a></h2>
<div class="desc std">
<?php echo $category->getDescription(); ?>
</div>
<?php if($category->getChildrenCount()){ ?>
<div>
<a href='<?php echo $this->getUrl('*/*/*',array('category_id'=>$category->getId()))?>'>View Sub Category</a>
</div>
<?php } ?>
</div>
</div>
</li>
<?php endforeach; ?>
</ol>
<script type="text/javascript">decorateList('products-list', 'none-recursive')</script>
<?php else: ?>
<!-- Grid Mode -->
<?php $_collectionSize = $collection->count() ?>
<?php
$_columnCount = $this->getColumnCount();
if(!$_columnCount){
$_columnCount = 3;
}
?>
<?php $i=0; foreach ($collection as $category):
$category = Mage::getModel('catalog/category')->load($category->getId());
?>
<?php if ($i++%$_columnCount==0): ?>
<ul class="products-grid">
<?php endif ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<a href="<?php echo $category->getUrl() ?>" title="<?php echo $this->stripTags($category->getName()); ?>" class="product-image"><img src="<?php echo $category->getImageUrl() ?>" width="135" height="135" alt="<?php echo $this->stripTags($category->getName()) ?>" /></a>
<h2 class="product-name"><a href="<?php echo $category->getUrl() ?>" title="<?php echo $category->getUrl() ?>"><?php echo $category->getName() ?></a></h2>
<?php if($category->getChildrenCount()){ ?>
<div>
<a href='<?php echo $this->getUrl('*/*/*',array('category_id'=>$category->getId()))?>'>View Sub Category</a>
</div>
<?php } ?>
</li>
<?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
</ul>
<?php endif ?>
<?php endforeach ?>
<script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
<?php endif; ?>
<?php echo $this->getToolbarHtml(); ?>
<?php else: ?>
<p><?php echo $this->__('The collection is empty.'); ?></p>
<?php endif ?>