Magento分层导航定制产品集合 [英] Magento layered navigation on custom product collection
问题描述
我一直在开发一个自定义模块Magento(版本1.8.0.0),显示一个产品的相关产品的列表。
为了实现这个我已经通过覆盖 Mage_Catalog_Block_Product_List
类创建了我自己的模块。
基本上这是如何工作: p>
从控制器捕获产品 entity_id
,我将产品存储在注册表中,以便我可以在我的自定义写为Block.php
这是填充产品集合的方法:
protected function _getProductCollection()
{
if(is_null($ this-> _productCollection)){
$ prod = Mage :: registry('selectedproduct') ;
$ this-> _productCollection = $ prod-> getRelatedProductCollection()
- > addAttributeToSelect('required_options')
- > addAttributeToFilter(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;
}
我还在我的自定义模块的布局.xml中添加了以下内容确保分层导航显示:
< reference name =left>
< block type =catalog / layer_viewname =catalog.leftnavafter =currencytemplate =catalog / layer / view.phtml/>
< / reference>
分层导航显示,但似乎它将所有产品作为集合而不是自定义集合
我也知道我可以使用 $ layer = Mage ::获取目录/图层getSingleton('catalog / layer');
图层类还有一个名为prepareProductCollection和setCollection的方法,
基本上我想为分层导航
感谢,
我只是设法实现我想要的。我已覆盖 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.
;)
这篇关于Magento分层导航定制产品集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!