16

我一直在为 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 的方法,但由于某种原因,我无法让它工作。

对此有什么帮助吗?

基本上,我希望为自定义集合中的产品提供分层导航。

谢谢,

4

1 回答 1

16

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

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

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

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

我还有一个方法可以让我在这两个类中设置这个“customProductCollection”。一旦它被设置,分层导航/类别的其余数据都基于这个集合。

;)

于 2011-09-21T09:11:55.537 回答