2

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?

3
  • Did you create sections.xml file? – Rohan Hapani Jan 22 '19 at 10:42
  • I am doing by override delete controller. Path:html/demo/vendor/magento/module-checkout/Controller/Cart/Delete.php – Jaimin Jan 22 '19 at 10:47
  • I don't have my custom controller so I don't think I need to create sections.xml because it used default magento sections.xml file. – Jaimin Jan 22 '19 at 10:48
3

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));
    }
}
1
  • Please do not use objectManager in vendor folder or your custom module. – Kushal Jan 23 '19 at 14:04
0

You can make a mixin to override it in your own theme:

define([
'jquery',
'Magento_Customer/js/customer-data',],function ($,customerData) {
return function(originalWidget) {

    $.widget('mage.sidebar', $.mage.sidebar, {

        _removeItemAfter: function (elem) {
            var productData = customerData.get('cart')().items.filter(function (item) {
                if(Number(elem.data('cart-item')) === Number(item['item_id'])) {
                    return true;
                }
            });

            $(document).trigger('ajax:removeFromCart', productData[0]['product_sku']);
        },
    });

    return $.mage.sidebar;
}});

in the requirejs-config you can add

var config = {
"config": {
    "mixins": {
        "Magento_Checkout/js/sidebar": {
            'Magento_Checkout/js/sidebar-mixin': true
        }
    }
}};

hope this helps!

4
  • Thanks for your reply. Could you tell me where I need to put this code? Could you provide me with a path? – Jaimin Jan 22 '19 at 10:41
  • There are two way: 1) Create your own module and use mixins 2) Override the file "vendor\magento\module-checkout\view\frontend\web\js\sidebar.js" in your own theme. – Amit Naraniwal Jan 22 '19 at 10:46
  • I have tried your code Amit but it is not updated cart quantity. I have get one url in which it removes all products from cart and it works but when I try to remove specific product on that code it gives same issue. Could you help me with this. URL: magento.stackexchange.com/questions/141314/… – Jaimin Jan 23 '19 at 4:53
  • @Jaimin, for this we need to debug the code. – Amit Naraniwal Jan 23 '19 at 5:01

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.