您可以添加要比较的产品。 如果产品未添加,我必须显示“添加到比较”链接,否则显示“比较”。 我必须检查产品是否在比较列表中。
我有list.phtml文件。
我尝试了这个,但这会在比较列表中添加所有产品。
$_productCollection = Mage::helper('catalog/product_compare')->getItemCollection()我可以遍历返回的产品,并可以检查产品是否在此集合中,但我正在寻找一个带有产品id或sku的单个调用,并相应地返回true或false 。
我也添加了这样的过滤器,但不起作用
$_productCollection = Mage::helper('catalog/product_compare')->getItemCollection() ->addAttributeToFilter('sku', $item->getSku());You can add product to compare. I have to show the link "Add to Compare" if the product is not added already otherwise show "Compare". I have to check if the product is in comparison list.
I have list.phtml file.
I tried this but this gives all the products added in comparison list.
$_productCollection = Mage::helper('catalog/product_compare')->getItemCollection()I can loop through the returned products and can check if the product is in this collection but I am looking for a single call which take the product id or sku and return true or false accordingly.
I also added the filter like this but does not work
$_productCollection = Mage::helper('catalog/product_compare')->getItemCollection() ->addAttributeToFilter('sku', $item->getSku());最满意答案
尝试使用
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; }因此,您可以按模型加载集合并在模板或自己的自定义帮助程序模型中过滤自身。
Try to use
Mage_Catalog_Model_Product_Compare_Listand its method:
getItemCollectionLike this:
$collection = Mage::getModel('catalog/product_compare_list')->getItemCollection(); $collection->.....Additional filters go here.Why helper didn't worked? Because collection is already loaded there:
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; }So you can load collection by model and filter itself in template or in your own custom helper - model.
更多推荐
发布评论