0

I have 2 items in cart suppose A & B. When customer delete item A from minicart then auto delete item B.

How the ajax will call 2 times, I have done this in controller's execute function (delete both item together when delete first item) but it is not working for me. enter image description here

Here is the code snippet: I have override this file Magento/Checkout/Controller/Sidebar/RemoveItem.php So my file is app/code/Vendor/Module/Controller/Sidebar/RemoveItem.php

 public function execute()
{
    if (!$this->getFormKeyValidator()->validate($this->getRequest())) {
        return $this->resultRedirectFactory->create()->setPath('*/cart/');
    }
    $itemId = (int)$this->getRequest()->getParam('item_id'); 
      

    //get all cart items
    $data = $this->resultCart->getQuote()->getAllItems();
    $valid = 1;
    foreach ($data as $item) {
       $sku = $item->getSku();
        if($sku == 'additional_item') //item B
        {
            $item_id = $item->getItemId();
        }
    }


    if(isset($item_id)){
        if($itemId == $item_id){
            $valid = 0;
        }
    }
    try {
        if($valid == 0){
             return $this->jsonResponse('We can\'t remove Sanitize item.');
        }
        if($valid){
             
            $this->sidebar->checkQuoteItem($itemId); 
            $this->sidebar->removeQuoteItem($itemId); //Removing item A

            if(isset($item_id)){ //removing the item B from here
                 $this->sidebar->checkQuoteItem($item_id);
                 $this->sidebar->removeQuoteItem($item_id);
             }

            return $this->jsonResponse();
        }

    } catch (\Magento\Framework\Exception\LocalizedException $e) {
        return $this->jsonResponse($e->getMessage());
    } catch (\Zend_Db_Exception $e) {
        $this->logger->critical($e);
        return $this->jsonResponse(__('An unspecified error occurred. Please contact us for assistance.'));
    } catch (\Exception $e) {
        $this->logger->critical($e);
        return $this->jsonResponse($e->getMessage());
    }
}
2
  • 1
    How are we supposed to help you if you dont post any code?!
    – Black
    Dec 1, 2020 at 15:40
  • @Black, I have updated my question.
    – Yogita
    Dec 2, 2020 at 5:19

1 Answer 1

0

In this segment:

 if(isset($item_id)){ //removing the item B from here
     $this->sidebar->checkQuoteItem($item_id);
     $this->sidebar->removeQuoteItem($item_id);
 }

Do you have something in place that changes $item_id to the "B" product you are trying to remove? I understand you may have just changed it out for clarity in the post, but wanted to make sure... As it is, you just removed $item_id, so it's unset (and will just get passed over) unless you update $item_id to be the "B" product.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.