Add remove product from compare programmatically in Magento2

In this tutorial we will discuss another technique of magento 2, we will learn How to add and remove product from compare in Magento 2. Obviously there are many ways and techniques of doing a task but if you are looking for a better and dynamic way then this tutorial may satisfy your needs. If a product is already added to the compare collection then on next click on that icon should remove that product from compare collection.

Remove product from compare

To remove product from compare collection , following piece of code will get the desired results.

protected  $_productloader;
protected $_compareItemFactory;
protected $_catalogProductCompareList;

public function __construct(
    \Magento\Catalog\Model\Product\Compare\ItemFactory $compareItemFactory,
    \Magento\Catalog\Model\Product\Compare\ListCompare $catalogProductCompareList,
    \Magento\Catalog\Model\ProductFactory $productloader
) {
    $this->_productloader = $productloader;
    $this->_compareItemFactory = $compareItemFactory;
    $this->_catalogProductCompareList = $catalogProductCompareList;
}

public function execute()
{
    $productId = (int)$this->getRequest()->getParam('product');
    $product=$this->_productloader->create()->load($productId);
    $compareitem=$this->_compareItemFactory->create()->loadByProduct($product);
    if($compareitem):
      $this->_catalogProductCompareList->removeProduct($product);
    endif;
}

As you can see magento 2 is using following code for removing the product from compare you can use this code too.

<a href="#" class="action tocompare" title="<?php echo $block->escapeHtml(__('Remove Compare')); ?>"
           aria-label="<?php echo $block->escapeHtml(__('Remove Compare')); ?>"
         data-post='<?php /* @escapeNotVerified */ echo $compareHelper->getPostDataRemove($_product); ?>'
                role="button">
      <span><?php /* @escapeNotVerified */ echo __('Remove Compare') ?></span>
 </a>

Add product to compare

To perform the add product to compare collection task, following code should be able to get the desired result.

protected  $_productloader;

protected $_catalogProductCompareList;

public function __construct(

    \Magento\Catalog\Model\Product\Compare\ListCompare $catalogProductCompareList,
    \Magento\Catalog\Model\ProductFactory $productloader
) {
    $this->_productloader = $productloader;
    $this->_catalogProductCompareList = $catalogProductCompareList;
}

public function execute()
{
    $productId = (int)$this->getRequest()->getParam('product');
    $product=$this->_productloader->create()->load($productId);

   
    $this->_catalogProductCompareList->addProduct($product);
   
}

A new file can be created to implement the above mentioned code and can be considered as a good technique but you can modify the existing file as well as per your requirements.

It is also a good practice to add your module and redirect it on your controllers, don’t try to edit the core file as it may get complicated.

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!

Leave a Reply