Magento – How to update Minicart count after remove product Programatically on cart page

magento2mini-cart

I have created automatically remove product with a specific ID Functionality.
So when the customer removes any product my specific ID product will be automatically removed.

I have done below code

Path: vendor/magento/module-checkout/Controller/Cart/Delete.php

<?php
/**
 *
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Checkout\Controller\Cart;

class Delete extends \Magento\Checkout\Controller\Cart
{
    /**
     * Delete shopping cart item action
     *
     * @return \Magento\Framework\Controller\Result\Redirect
     */
    public function execute()
    {
        if (!$this->_formKeyValidator->validate($this->getRequest())) {
            return $this->resultRedirectFactory->create()->setPath('*/*/');
        }

        $id = (int)$this->getRequest()->getParam('id');
        if ($id) {
            try {
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $cart = $objectManager->get('\Magento\Checkout\Model\Cart');                 
                // retrieve quote items collection
                $itemsCollection = $cart->getQuote()->getItemsCollection();                 
                // get array of all items what can be display directly
                $itemsVisible = $cart->getQuote()->getAllVisibleItems();                 
                // retrieve quote items array
                $items = $cart->getQuote()->getAllItems();
                $cartid = "";
                foreach($items as $item)
                {
                    if($item->getProductId() == 110009)
                    {
                        $cartid = $item->getItemId();        
                    }

                }
                $this->cart->removeItem($id)->save();
                if($cartid != "")
                {
                    $this->cart->removeItem($cartid)->save();
                }
            } catch (\Exception $e) {
                $this->messageManager->addError(__('We can\'t remove the item.'));
                $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
            }
        }
        $defaultUrl = $this->_objectManager->create(\Magento\Framework\UrlInterface::class)->getUrl('*/*');
        return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl($defaultUrl));
    }
}

My Issue:

When the customer removes any product my specific ID product will be automatically removed but my mini cart count is only removed only one product.

For Instance: There is 3 product in cart including my specific ID product. Now if Customer removes one of the product it removes that product and my specified ID product too. But my cart count will deduct only 1 product.

So Expected result of Minicart should be 1 but it shows 2.

Could anyone help me that how can I set proper count in minicart?

Best Answer

I have solved Issue. Below is my code.

<?php
/**
 *
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Checkout\Controller\Cart;

class Delete extends \Magento\Checkout\Controller\Cart
{
    /**
     * Delete shopping cart item action
     *
     * @return \Magento\Framework\Controller\Result\Redirect
     */
    public function execute()
    {
        if (!$this->_formKeyValidator->validate($this->getRequest())) {
            return $this->resultRedirectFactory->create()->setPath('*/*/');
        }

        $id = (int)$this->getRequest()->getParam('id');
        if ($id) {
            try {
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                $cart = $objectManager->get('\Magento\Checkout\Model\Cart');                 
                // retrieve quote items collection
                $itemsCollection = $cart->getQuote()->getItemsCollection();                 
                // get array of all items what can be display directly
                $itemsVisible = $cart->getQuote()->getAllVisibleItems();                 
                // retrieve quote items array
                $items = $cart->getQuote()->getAllItems();
                $cartid = "";
                foreach($items as $item)
                {
                    if($item->getProductId() == 110009)
                    {
                        $cartid = $item->getItemId();        
                    }

                }
                $this->cart->removeItem($id);
                if($cartid != "")
                {
                    $this->cart->removeItem($cartid)->save();
                }
            } catch (\Exception $e) {
                $this->messageManager->addError(__('We can\'t remove the item.'));
                $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
            }
        }
        $defaultUrl = $this->_objectManager->create(\Magento\Framework\UrlInterface::class)->getUrl('*/*');
        return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl($defaultUrl));
    }
}
Related Topic