如何检查产品是否在 magento 比较列表中

发布于 2024-12-17 17:29:14 字数 591 浏览 0 评论 0原文

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

我有 list.phtml 文件。

我尝试过这个,但这给出了比较列表中添加的所有产品。

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

我可以循环访问返回的产品,并可以检查该产品是否在此集合中,但我正在寻找一个获取产品 idsku 并返回 truefalse 相应地。

我也像这样添加了过滤器,但不起作用

$_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());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坠似风落 2024-12-24 17:29:14

尝试使用

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文