2011-11-25 85 views
3

您可以添加產品進行比較。如果產品未添加,則必須顯示「添加到比較」鏈接,否則顯示「比較」。我必須檢查產品是否在比較列表中。如何檢查產品是否在比較列表中magento

我有list.phtml文件。

我試過這個,但是這給出了比較列表中添加的所有產品。

$_productCollection = Mage::helper('catalog/product_compare')->getItemCollection() 

我可以通過返回的產品循環,可以檢查該產品是否在此集合中,但是我正在尋找一個單一的調用,它以產品idsku並返回truefalse相應。

我還添加了過濾器這樣可是不行的

$_productCollection = Mage::helper('catalog/product_compare')->getItemCollection() 
      ->addAttributeToFilter('sku', $item->getSku()); 

回答

2

嘗試使用

Mage_Catalog_Model_Product_Compare_List 

及其方法:

getItemCollection 

像這樣:

$collection = Mage::getModel('catalog/product_compare_list')->getItemCollection(); 
$collection->.....Additional filters go here. 

爲什麼助手沒有工作?由於收集已經加載有:

V 1.6

public function getItemCollection() 
{ 
    if (!$this->_itemCollection) { 
     $this->_itemCollection = Mage::getResourceModel('catalog/product_compare_item_collection') 
      ->useProductItem(true) 
      ->setStoreId(Mage::app()->getStore()->getId()); 

     if (Mage::getSingleton('customer/session')->isLoggedIn()) { 
      $this->_itemCollection->setCustomerId(Mage::getSingleton('customer/session')->getCustomerId()); 
     } elseif ($this->_customerId) { 
      $this->_itemCollection->setCustomerId($this->_customerId); 
     } else { 
      $this->_itemCollection->setVisitorId(Mage::getSingleton('log/visitor')->getId()); 
     } 

     Mage::getSingleton('catalog/product_visibility') 
      ->addVisibleInSiteFilterToCollection($this->_itemCollection); 

     /* Price data is added to consider item stock status using price index */ 
     $this->_itemCollection->addPriceData(); 

     $this->_itemCollection->addAttributeToSelect('name') 
      ->addUrlRewrite() 
      ->load(); 

     /* update compare items count */ 
     $this->_getSession()->setCatalogCompareItemsCount(count($this->_itemCollection)); 
    } 

    return $this->_itemCollection; 
} 

所以,你可以通過模型加載收集和模板或自己的自定義過濾幫手本身 - 模型。

相關問題