Magento layered navigation on custom product collection

我是研究僧i 提交于 2019-11-27 13:52:56

问题


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,


回答1:


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.

;)



来源:https://stackoverflow.com/questions/5726891/magento-layered-navigation-on-custom-product-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!