Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Cancel.php
Go to the documentation of this file.
1 <?php
8 
10 
12 {
18  public function execute()
19  {
20  try {
21  $this->_initToken(false);
22  // TODO verify if this logic of order cancellation is deprecated
23  // if there is an order - cancel it
24  $orderId = $this->_getCheckoutSession()->getLastOrderId();
26  $order = $orderId ? $this->_orderFactory->create()->load($orderId) : false;
27  if ($order && $order->getId() && $order->getQuoteId() == $this->_getCheckoutSession()->getQuoteId()) {
28  $order->cancel()->save();
29  $this->_getCheckoutSession()
30  ->unsLastQuoteId()
31  ->unsLastSuccessQuoteId()
32  ->unsLastOrderId()
33  ->unsLastRealOrderId();
34  $this->messageManager->addSuccessMessage(
35  __('Express Checkout and Order have been canceled.')
36  );
37  } else {
38  $this->messageManager->addSuccessMessage(
39  __('Express Checkout has been canceled.')
40  );
41  }
42  } catch (\Magento\Framework\Exception\LocalizedException $e) {
43  $this->messageManager->addExceptionMessage($e, $e->getMessage());
44  } catch (\Exception $e) {
45  $this->messageManager->addExceptionMessage($e, __('Unable to cancel Express Checkout'));
46  }
47 
49  $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
50  return $resultRedirect->setPath('checkout/cart');
51  }
52 }
$order
Definition: order.php:55
__()
Definition: __.php:13