且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Magento分层导航定制产品集合

更新时间:2023-11-30 09:57:16

我只是设法实现我想要的。我已覆盖 Mage_Catalog_Model_Layer 类和 Mage_Catalog_Model_Category



两个现在都有一个新的变量$ _customCollection: protected $ _customProductCollection;



我覆盖了getProductCollection在这两个类中,我在方法的开头添加了这个:

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



我还有一个方法允许我设置这个customProductCollection这些类。一旦设置,分层导航/类别的其余数据基于此集合。



;)


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,

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.

;)