2011-11-10 47 views
0

我想在我的Magento產品列表中顯示「建議」。我提出了一個屬性「建議」,它是「是/否」和全局活動。現在在列表中,我想首先提出建議,然後是一些文本和內容,然後是其他產品。產品列表基於Magento中的屬性

我想它是這樣的:

$_productCollection=$this->getLoadedProductCollection() 
/* .... */ 
$_productCollection->clear()->addAttributeToFilter('suggestion', 1)->load(); 

但在例外此結束:

你不能定義一個相關名 '_price_rule' 不止一次

現在問題是,如何解決這個問題?

回答

0

好的,我的解決方案是一個自定義模塊,它擴展了列表功能。我增加了以下文件:

/app/code/local/Mage/Catalog/Block/Product/List.php

用下面的擴展碼:

protected function _getProductCollectionSuggestion() 
{ 
    $layer = Mage::getSingleton('catalog/layer'); 
    /* @var $layer Mage_Catalog_Model_Layer */ 
    if ($this->getShowRootCategory()) { 
     $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId()); 
    } 

    // if this is a product view page 
    if (Mage::registry('product')) { 
     // get collection of categories this product is associated with 
     $categories = Mage::registry('product')->getCategoryCollection() 
      ->setPage(1, 1) 
      ->load(); 
     // if the product is associated with any category 
     if ($categories->count()) { 
      // show products from this category 
      $this->setCategoryId(current($categories->getIterator())); 
     } 
    } 

    $origCategory = null; 
    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->_productCollection->addAttributeToFilter('suggestion', 1); 
    if($this->_productCollection->count()) { 
     foreach($this->_productCollection as $_productKey => $_product) { 
      if($_product->getSuggestion() == 0 || !$_product->getSuggestion()) { 

      } 
     } 
    } 

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

    if ($origCategory) { 
     $layer->setCurrentCategory($origCategory); 
    } 
    return $this->_productCollection; 
} 

然後我在List.phtml中加載了這個方法,它工作:) 感謝您閱讀!也許這段代碼可以幫助某人