我一直在为 Magento(版本 1.8.0.0)开发一个自定义模块,该模块显示某个产品的相关产品列表。
为了实现这一点,我通过覆盖Mage_Catalog_Block_Product_List
类创建了自己的模块。
基本上这是它的工作原理:
从控制器中,我捕获产品entity_id
并将产品存储在注册表中,以便我可以在名为 list.php 的自定义编写块中使用它
这是填充产品集合的方法:
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;
}
我还在自定义模块的布局 .xml 中添加了以下内容,以确保显示分层导航:
<reference name="left">
<block type="catalog/layer_view" name="catalog.leftnav" after="currency" template="catalog/layer/view.phtml"/>
</reference>
分层导航显示,但似乎将所有产品作为集合而不是我在上面添加的方法中使用的自定义集合。
我也知道我可以使用它来获取目录/图层$layer = Mage::getSingleton('catalog/layer');
图层类还有一个名为 prepareProductCollection 和 setCollection 的方法,但由于某种原因,我无法让它工作。
对此有什么帮助吗?
基本上,我希望为自定义集合中的产品提供分层导航。
谢谢,