2011-11-24 50 views
0

我的Magento主頁目前有這個代碼片段,顯示所有產品都很好。如何使用Magento塊顯示多個類別

{{block type="catalog/product_list" category_id="2" template="catalog/product/list.phtml"}} 

粗略地說,我的類別樹類似於這樣

id 2 (root cat) 
-> id 3 
-> id 4 
-> id 5 

由於每個產品我補充的是編號爲2的孩子 - 每一個產品顯示了在主頁上。我所追求的是一種解決方案,它允許我從主頁產品列表中排除特定的ID(類別)。

我試過這個片段下面沒有成功:

{{block type="catalog/product_list" category_id="2,3,5" template="catalog/product/list.phtml"}} 

回答

2

您的代碼{{block type="catalog/product_list" category_id="2,3,5" template="catalog/product/list.phtml"}}不會起作用,因爲該塊Mage_Catalog_Block_Product_List負荷只有一個類別$category = Mage::getModel('catalog/category')->load($this->getCategoryId());

我看到你的問題,有兩種解決方案,您可以不止一次使用塊與不同類別的ID:

{{block type="catalog/product_list" category_id="2" template="catalog/product/list.phtml"}} 
{{block type="catalog/product_list" category_id="3" template="catalog/product/list.phtml"}} 
{{block type="catalog/product_list" category_id="5" template="catalog/product/list.phtml"}} 

或覆蓋塊Mage_Catalog_Block_Product_List並改變這部分的行爲

 if ($this->getCategoryId()) { 
      $category = Mage::getModel('catalog/category')->load($this->getCategoryId()); 
      if ($category->getId()) { 
       $origCategory = $layer->getCurrentCategory(); 
       $layer->setCurrentCategory($category); 
      } 
     } 
     $this->_productCollection = $layer->getProductCollection(); 

     $this->prepareSortableFieldsByCategory($layer->getCurrentCategory()); 

     if ($origCategory) { 
      $layer->setCurrentCategory($origCategory); 
     } 
+0

謝謝你的回答。我將在稍後使用該方法 - 就目前而言 - 時間不在我身邊。謝謝! – tjw