1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Checkout\Controller\Cart;
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
/**
* Action Delete.
*
* Deletes item from cart.
*/
class Delete extends \Magento\Checkout\Controller\Cart implements HttpPostActionInterface
{
/**
* 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 {
$this->cart->removeItem($id);
// We should set Totals to be recollected once more because of Cart model as usually is loading
// before action executing and in case when triggerRecollect setted as true recollecting will
// executed and the flag will be true already.
$this->cart->getQuote()->setTotalsCollectedFlag(false);
$this->cart->save();
} catch (\Exception $e) {
$this->messageManager->addErrorMessage(__('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));
}
}