5

Looking for a better way to add and remove the product on click of same icon.

If a product is already added to the compare collection then on next click of that icon, It should remove the product from compare collection.

3 Answers 3

7
+50
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);
    else:
    $this->_catalogProductCompareList->addProduct($product);
    endif;
}
6
  • For this need to create new file or change in existing.please show path. Commented Mar 8, 2018 at 4:59
  • @KetanKpBorada create new is good but you can modify the existing as your requirement. Commented Mar 8, 2018 at 5:00
  • please show full path with file name. Commented Mar 8, 2018 at 5:14
  • @KetanKpBorada kindly add your module ad redirect it on your controllers don't try to edit the core file. Commented Mar 8, 2018 at 5:16
  • 1
    @ToanNguyen it was write code for test but thanks for notice and 22k not mean i am best. Commented Mar 9, 2018 at 6:56
3

You can remove product from compare collection using below code:

<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>
1
  • where is added this code please specify on file and path
    – Rv Singh
    Commented Aug 30, 2019 at 11:45
0

This work for me,

    public function __construct(
        \Magento\Customer\Model\Session\Proxy              $sessionProxy,
        \Magento\Catalog\Model\Product\Compare\ListCompare $listCompare,
        \Magento\Catalog\Model\Product\Compare\ItemFactory $compareItemFactory,
        \Magento\Catalog\CustomerData\CompareProducts      $compareProducts,
        \Magento\Catalog\Model\ProductFactory              $productloader
    ) {
        $this->_productloader = $productloader;
        $this->_compareItemFactory = $compareItemFactory;
        $this->compareProducts = $compareProducts;
        $this->sessionProxy = $sessionProxy;
        $this->listCompare = $listCompare;
    }

   public function getCompareList()
    {
        return $this->compareProducts->getSectionData();
    }

    public function removePrd(){
    $listt = $this->getCompareList();
        foreach ($listt['items'] as $key => $comparelist) {

            $prdName = $comparelist['name'];
            $prdidd = $comparelist['id'];

            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $product = $objectManager->create('Magento\Catalog\Model\Product')->load($prdidd);
            $product = $this->_productloader->create()->load($prdidd);
            $compareitem = $this->_compareItemFactory->create()->loadByProduct($product);

            if (!empty($compareitem)):
                $compareitem->delete($product);
            endif;
        }

Thank you.

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.