2

I need to set limit to the compared product, for example: max_limit 4, But now it is adding Number of products to compare list. Kindly suggest.

3
  • developersushant.wordpress.com/2016/01/25/… Commented Feb 13, 2017 at 7:24
  • Thanks, I went through this link previously but this code is in magento 1.9 as i need in magento 2 version
    – user48923
    Commented Feb 13, 2017 at 7:28
  • have you check set limit from here, Admin -> Stores -> Configuration -> Catalog -> Catalog -> Recently Viewed/Compared Products -> Default Recently Compared Products Count? Commented Feb 13, 2017 at 9:26

2 Answers 2

3

We can try with Plugin. We use Magento\Catalog\Helper\Product\Compare to get the current compared products.

app/code/Company/Catalog/etc/frontend/di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Catalog\Controller\Product\Compare\Add">
        <plugin name="LimitToCompareProducts"
                type="Company\Catalog\Model\Plugin\Compare\LimitToCompareProducts"/>
    </type>
</config>

app/code/Company/Catalog/Model/Plugin/Compare/LimitToCompareProducts.php

<?php

namespace Company\Catalog\Model\Plugin\Compare;

use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\Message\ManagerInterface;
use Magento\Catalog\Helper\Product\Compare;

class LimitToCompareProducts
{
    const LIMIT_TO_COMPARE_PRODUCTS = 3;

    /**
     * @var \Magento\Framework\Message\ManagerInterface
     */
    protected $messageManager;

    /**
     * @var RedirectFactory
     */
    protected $resultRedirectFactory;

    /** @var Compare */
    protected $helper;

    /**
     * RestrictCustomerEmail constructor.
     * @param Compare $helper
     * @param RedirectFactory $redirectFactory
     * @param ManagerInterface $messageManager
     */
    public function __construct(
        RedirectFactory $redirectFactory,
        Compare $helper,
        ManagerInterface $messageManager
    )
    {
        $this->helper = $helper;
        $this->resultRedirectFactory = $redirectFactory;
        $this->messageManager = $messageManager;
    }

     public function aroundExecute(
    \Magento\Catalog\Controller\Product\Compare\Add $subject,
    \Closure $proceed
    ){

      $count = $this->helper->getItemCount();
      if($count > self::LIMIT_TO_COMPARE_PRODUCTS) {
        $this->messageManager->addErrorMessage(
            'You can add the compared products under 3 item(s)'
        );

        /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
        return $resultRedirect->setRefererOrBaseUrl();
      }

      return $proceed();
   }
}
3
  • 1
    What about Admin -> Stores -> Configuration -> Catalog -> Catalog -> Recently Viewed/Compared Products -> Default Recently Compared Products Count?
    – Jackson
    Commented Feb 13, 2017 at 8:51
  • tried this but not working for magento 2.4 Commented Oct 30, 2020 at 11:24
  • Hello @Khoa I used this method but the message gets displayed on another page or on refresh
    – Arjun
    Commented Mar 18, 2021 at 12:57
0

@ankit

What about Admin -> Stores -> Configuration -> Catalog -> Catalog -> Recently Viewed/Compared Products -> Default Recently Compared Products Count?

This does not limit the quantity of items on your compare.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.