Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdatePost.php
Go to the documentation of this file.
1 <?php
7 
11 
16 {
20  private $quantityProcessor;
21 
31  public function __construct(
32  \Magento\Framework\App\Action\Context $context,
33  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
34  \Magento\Checkout\Model\Session $checkoutSession,
36  \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
37  \Magento\Checkout\Model\Cart $cart,
38  RequestQuantityProcessor $quantityProcessor = null
39  ) {
40  parent::__construct(
41  $context,
42  $scopeConfig,
43  $checkoutSession,
45  $formKeyValidator,
46  $cart
47  );
48 
49  $this->quantityProcessor = $quantityProcessor ?: $this->_objectManager->get(RequestQuantityProcessor::class);
50  }
51 
57  protected function _emptyShoppingCart()
58  {
59  try {
60  $this->cart->truncate()->save();
61  } catch (\Magento\Framework\Exception\LocalizedException $exception) {
62  $this->messageManager->addErrorMessage($exception->getMessage());
63  } catch (\Exception $exception) {
64  $this->messageManager->addExceptionMessage($exception, __('We can\'t update the shopping cart.'));
65  }
66  }
67 
73  protected function _updateShoppingCart()
74  {
75  try {
76  $cartData = $this->getRequest()->getParam('cart');
77  if (is_array($cartData)) {
78  if (!$this->cart->getCustomerSession()->getCustomerId() && $this->cart->getQuote()->getCustomerId()) {
79  $this->cart->getQuote()->setCustomerId(null);
80  }
81  $cartData = $this->quantityProcessor->process($cartData);
82  $cartData = $this->cart->suggestItemsQty($cartData);
83  $this->cart->updateItems($cartData)->save();
84  }
85  } catch (\Magento\Framework\Exception\LocalizedException $e) {
86  $this->messageManager->addErrorMessage(
87  $this->_objectManager->get(\Magento\Framework\Escaper::class)->escapeHtml($e->getMessage())
88  );
89  } catch (\Exception $e) {
90  $this->messageManager->addExceptionMessage($e, __('We can\'t update the shopping cart.'));
91  $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
92  }
93  }
94 
100  public function execute()
101  {
102  if (!$this->_formKeyValidator->validate($this->getRequest())) {
103  return $this->resultRedirectFactory->create()->setPath('*/*/');
104  }
105 
106  $updateAction = (string)$this->getRequest()->getParam('update_cart_action');
107 
108  switch ($updateAction) {
109  case 'empty_cart':
110  $this->_emptyShoppingCart();
111  break;
112  case 'update_qty':
113  $this->_updateShoppingCart();
114  break;
115  default:
116  $this->_updateShoppingCart();
117  }
118 
119  return $this->_goBack();
120  }
121 }
_emptyShoppingCart()
Definition: UpdatePost.php:57
$storeManager
__()
Definition: __.php:13
_updateShoppingCart()
Definition: UpdatePost.php:73
_goBack($backUrl=null)
Definition: Cart.php:73
Definition: UpdatePost.php:15
__construct(\Magento\Framework\App\Action\Context $context, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator, \Magento\Checkout\Model\Cart $cart, RequestQuantityProcessor $quantityProcessor=null)
Definition: UpdatePost.php:31
execute()
Definition: UpdatePost.php:100