Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 589
Kenny Avatar asked Apr 20 '11 07:04

Kenny


People also ask

How do I use custom conditions for product collection in Magento 2?

For Apply custom condition before product collection loaded, you need to create a di. xml file for plugin definition and plugin PHP file for add your custom logic. Under the di. xml file, You need to add plugin definition to create plugin beforeLoad( ) method.

How do I keep all filters visible in Magento 2 layered navigation?

The Catalog Input Type for Store Owner property of each attribute must be set to “Dropdown,” “Multiple Select,” or “Price.” To make the attributes filterable, the Use in Layered Navigation property of each must be set to either “Filterable (with results)” or “Filterable (no results).”


1 Answers

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.

;)

like image 60
Kenny Avatar answered Oct 05 '22 09:10

Kenny