1

Case : Suppose i have products Shirts and Electronics, now if i add Shirt A and Mobile B to compare it allows but i want to restrict compare if same attributes sets .

So i user add Shirt to compare and trying to add Electronics to compare he will be shown error you can not compare Shirt to electronics.

How can this be achieved, please help us on it .

2

Here is complete solution.

di.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<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="restrict_compare_to_add" type="VendorName\ModuleName\Plugin\CheckCompare" />
</type>

CheckCompare.php

<?php
namespace Catalog\Compare\Plugin;

class CheckCompare
{
/**
 * @var \Magento\Catalog\CustomerData\CompareProducts
 */
protected $compareProducts;

/**
 * @var \Magento\Framework\App\RequestInterface
 */
protected $request;

/**
 * @var \Magento\Catalog\Api\ProductRepositoryInterface
 */
protected $productRepository;

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

/**
 * @var \Magento\Framework\Controller\ResultFactory
 */
protected $resultFactory;

/**
 * @var \Magento\Framework\App\Response\RedirectInterface
 */
protected $redirect;

/**
 * @param \Magento\Catalog\CustomerData\CompareProducts $compareProducts,
 * @param \Magento\Framework\App\RequestInterface $request,
 * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
 * @param \Magento\Framework\Message\ManagerInterface $messageManager,
 * @param \Magento\Framework\Controller\ResultFactory $resultFactory,     
 * @param \Magento\Framework\App\Response\RedirectInterface $redirect 
 */
 public function __construct(
    \Magento\Catalog\CustomerData\CompareProducts $compareProducts,
    \Magento\Framework\App\RequestInterface $request,
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
    \Magento\Framework\Message\ManagerInterface $messageManager,
    \Magento\Framework\Controller\ResultFactory $resultFactory,     
    \Magento\Framework\App\Response\RedirectInterface $redirect     
    ) 
{
    $this->compareProducts = $compareProducts;
    $this->request = $request;
    $this->productRepository = $productRepository;
    $this->messageManager = $messageManager;
    $this->resultFactory = $resultFactory;
    $this->redirect = $redirect;
}

/**
 * check if product can be add to compare list
 */
 public function aroundExecute(\Magento\Catalog\Controller\Product\Compare\Add $subject, $proceed) 
 {
    $resultRedirect = $this->resultFactory->create(
            \Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT
        );
    $productIdtoBeAdd = $this->request->getParam('product');
    $productRepository = $this->productRepository->getById($productIdtoBeAdd);
    $newAtrributeSet = $productRepository->getAttributeSetId();
    $compareCollection = $this->compareProducts->getSectionData();
    $oldAtrributeSets = [];
    foreach($compareCollection['items'] as $compareList){
        $oldAtrributeSets[] = $this->productRepository->getById($compareList['id'])->getAttributeSetId();
    } 
    if(count($oldAtrributeSets) > 0 && !in_array($newAtrributeSet, $oldAtrributeSets))
    {
       $this->messageManager->addWarningMessage(__('You cannot add product %1 in comparison list as it belongs to different attribute set.', $productRepository->getName()));
       $result = $resultRedirect->setUrl($this->redirect->getRefererUrl());
    } else {
        $result = $proceed();
    }

    return $result;
 }

}
| improve this answer | |
  • bro i am bit new to magento can u explain me from start , also it will be nice if u help me i will pay for your time – user1799722 Oct 12 '18 at 18:09
  • can u please help me with it – user1799722 Oct 12 '18 at 19:03

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.