2011-04-20 78 views
15

我一直在为Magento(版本1.8.0.0)定制模块,该模块显示某个产品的相关产品列表。自定义产品集合上的Magento分层导航

为了达到这个目的,我创建了自己的模块,覆盖了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方法,但由于某种原因,我无法得到它的工作得到了目录/层。

对此有何帮助?

基本上我想要在自定义集合中的产品的分层导航。

谢谢,

回答

15

我只是设法达到我想要的。我已经覆盖两个Mage_Catalog_Model_Layer类和Mage_Catalog_Model_Category

双方现在都要求一个新的变量$ _customCollection:protected $_customProductCollection;

我已经在这两个类覆盖了getProductCollection()和我说这方法的开头:

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

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

;)

+2

这是什么设置方法,你从哪里调用它? – easymoden00b 2015-02-17 20:54:39