2011-11-25 87 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; 
} 

所以,你可以通过模型加载收集和模板或自己的自定义过滤帮手本身 - 模型。

相关问题