Here is the plugin i created for limiting 3 products can be added into comparison. But it not loading, add one know what is the problem?


<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    <type name="Magento\Catalog\Controller\Product\Compare\Add">
        <plugin name="LimitToCompareProducts"


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Custom_TestCompare" setup_version="1.0.0"/>



namespace Custom\TestCompare\Plugin;

    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) {
                    '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();



First thing I would do is add a dependency on Magento_Catalog which should change the load order of the modules so that yours loads after Magento_Catalog:

In etc/module.xml:

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Custom_TestCompare" setup_version="1.0.0">
            <module name="Magento_Catalog"/>

Then run php bin/magento setup:upgrade so that your module load order changes in app/etc/config.php.

Outside of that, did you do the following?

  • Verify that your module is enabled in app/etc/config.php (should see Custom_TestCompare => 1 in the module list)
  • Verify the path to the module is app/code/Custom/TestCompare

Compare Limit Logic

Also noticed that you check if $count > 3. Should it be $count >= 3 to prevent a customer from being allowed to add 4 items before your plugin limits?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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