collectionsmagentonavigationproductlayered

Magento layered navigation on custom product collection


I have been working on a custom module for Magento (ver. 1.8.0.0) that shows a list of related products of a certain product.

In order to achieve this I have created my own module by overwriting the Mage_Catalog_Block_Product_List class.

Basically here's how it works:

From a controller I catch the products entity_id and I store the product in the registry so I can use it inside my custom written Block which is called list.php

Here is the method that fills the product collection:

protected function _getProductCollection()
{
    if (is_null($this->_productCollection)) {
        $prod = Mage::registry('chosenproduct');
        $this->_productCollection = $prod->getRelatedProductCollection()
            ->addAttributeToSelect('required_options')
            ->addAttributeToFilter(array(array('attribute'=>'accessory_manufacturer','neq'=>false)))
            ->addAttributeToSort('position', 'asc')
            ->addStoreFilter()
            ->setPageSize(30)
            ->setCurPage(1);
        ;

        $this->_addProductAttributesAndPrices($this->_productCollection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_productCollection);
        $this->setProductCollection($this->_productCollection);
    }

    return $this->_productCollection;
}

I also added the following in the layout .xml of my custom module to make sure the layered navigation shows:

<reference name="left">
        <block type="catalog/layer_view" name="catalog.leftnav" after="currency" template="catalog/layer/view.phtml"/>
    </reference>

The layered navigation shows, but it seems that it is taking all products as collection instead of the custom collection that is used in the method I added above.

I also know that I can get the catalog/layer using this $layer = Mage::getSingleton('catalog/layer');

The layer class also has a method called prepareProductCollection and setCollection but for some reason I can't get it to work.

Any help on this?

Basically I want to have the layered navigation for the products that are in the custom collection.

Thanks,


Solution

  • I just managed to achieve what I wanted. I have overwritten both the Mage_Catalog_Model_Layer class and the Mage_Catalog_Model_Category

    Both now have a new variable called $_customCollection: protected $_customProductCollection;

    I have overwritten the getProductCollection() in both classes and I added this in the beginning of the method:

    if(isset($this->_customProductCollection)){
            return $this->_customProductCollection;
        }
    

    I have also a method that allows me to set this "customProductCollection" inside both these classes. Once It's set, the rest of the data of the layered navigation/category is based on this collection.

    ;)