Magento – layered navigation for custom product collection on custom page

layered-navigationmagento-1.8

I'm working on a module where some products have an attribute named designer. and for every designer, all products by him displayed as catalog category view. So far I'm able to filter the products collection by designer attribute and successfully display them as the catalog category view page.

But now the problem is, I need to display a layered navigation for those product which may be filtered by price and all.

Is there a way that I could add layered navigation in that filtered product collection?

Best Answer

Mohammad found an answer on StackOverflow which has since been deleted. Here is that answer:

Finally I found the solution for custom Layered navigation for custom product collection. Here are the changes what I have did to sort-out this issue.

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 extends Mage_Core_Block_Template and it has just two method one for _prepareLayout & for _prepareLayout.

b) Mymodule_Onsale_Block_Product_List extends Mage_Catalog_Block_Product_List and it has 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 changes in controller, 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 me correct product listing with layered navigation.