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.
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());
}
}