2011-04-20 48 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