Create Layered Navigation for custom product collection

Here are the changes what you need do.

1) Created one custom module for custom page. My custom page URL was http://{magento_root}/onsale.
2) Set below Layout XML file for my custom page layout (‘onsale‘ is my custom attribute).

<onsale_index_index translate=”label”>
<reference name=”left”>
<block type=”onsale/layer_view” name=”customlayer” after=”currency” template=”catalog/layer/view.phtml”/>
</reference>
<reference name=”content”>
<block type=”core/template” name=”category.products” template=”onsale/category/view.phtml”>
<block type=”onsale/product_list” name=”product_list” template=”catalog/product/list.phtml”>

<block type=”catalog/product_list_toolbar” name=”product_list_toolbar” template=”catalog/product/list/toolbar.phtml”>
<block type=”page/html_pager” name=”product_list_toolbar_pager”/>
</block>
<action method=”addColumnCountLayoutDepend”><layout>empty</layout><count>6</count></action>
<action method=”addColumnCountLayoutDepend”><layout>one_column</layout><count>5</count></action>
<action method=”addColumnCountLayoutDepend”><layout>two_columns_left</layout><count>4</count></action>
<action method=”addColumnCountLayoutDepend”><layout>two_columns_right</layout><count>4</count></action>
<action method=”addColumnCountLayoutDepend”><layout>three_columns</layout><count>3</count></action>
<action method=”setToolbarBlockName”><name>product_list_toolbar</name></action>
</block>
</block>
</reference>
</onsale_index_index>

3) In ‘onsale/category/view.phtml‘ i have just set page title & called product listing ‘$this->getChildHtml(‘product_list’);‘.

4) Now i have three block file, a) onsale, b) product->list, c) layer->view.

a) ‘Mymodule_Onsale_Block_Onsale’ is extend to ‘Mage_Core_Block_Template’ and it has just two method one for _prepareLayout & for _prepareLayout.

b) ‘Mymodule_Onsale_Block_Product_List‘ is extend to ‘Mage_Catalog_Block_Product_List‘ it is just one method which is as below.

protected function _getProductCollection()
{

if (is_null($this->_productCollection))
{
$layer = $this->getLayer();
$productCollection = $layer->getProductCollection();
$this->_productCollection = $productCollection;
}
return $this->_productCollection;
}

c) layer->view block has one method ‘getLayer()‘ as below.

public function getLayer()
{
return Mage::getSingleton(‘onsale/layer’);
}

5) No any changes to controller is just call to Layout & Render.

6) Now for model file. I have just one model file for layer with ‘getProductCollection‘ method as below. which extend to ‘Mage_Catalog_Model_Layer’.

class name Mymodule_Onsale_Model_Layer extends Mage_Catalog_Model_Layer

public function getProductCollection()
{

if (isset($this->_productCollections[$this->getCurrentCategory()->getId()])) {
$collection = $this->_productCollections[$this->getCurrentCategory()->getId()];
} else {

$collection = Mage::getResourceModel(‘catalog/product_collection’)->addAttributeToSelect(‘*’);
$collection->addFieldToFilter(‘on_sale’,array(‘eq’=>’125’));

$this->prepareProductCollection($collection);
$this->_productCollections[$this->getCurrentCategory()->getId()] = $collection;
}
return $collection;
}

7) One last important, Please set ‘No‘ to ‘Use Flat Catalog Category‘ & ‘Use Flat Catalog Product‘ at System -> configuration -> Catalog -> fontend.

It show correct product listing with layered navigation.

One thought on “Create Layered Navigation for custom product collection

  1. Thanks very interesting blog!

Comments are closed.