如何检查产品是否在比较列表magento中(How do I check if a product is in comparison list magento)

编程入门 行业动态 更新时间:2024-10-28 10:25:45
如何检查产品是否在比较列表magento中(How do I check if a product is in comparison list magento)

您可以添加要比较的产品。 如果产品未添加,我必须显示“添加到比较”链接,否则显示“比较”。 我必须检查产品是否在比较列表中。

我有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_List

and its method:

getItemCollection

Like 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.

更多推荐

本文发布于:2023-08-03 08:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385320.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   产品   magento   check   comparison

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!