Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateItemOptions.php
Go to the documentation of this file.
1 <?php
9 
10 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
11 
13 {
21  public function execute()
22  {
23  $id = (int)$this->getRequest()->getParam('id');
24  $params = $this->getRequest()->getParams();
25 
26  if (!isset($params['options'])) {
27  $params['options'] = [];
28  }
29  try {
30  if (isset($params['qty'])) {
31  $filter = new \Zend_Filter_LocalizedToNormalized(
32  ['locale' => $this->_objectManager->get(
33  \Magento\Framework\Locale\ResolverInterface::class
34  )->getLocale()]
35  );
36  $params['qty'] = $filter->filter($params['qty']);
37  }
38 
39  $quoteItem = $this->cart->getQuote()->getItemById($id);
40  if (!$quoteItem) {
41  throw new \Magento\Framework\Exception\LocalizedException(
42  __("The quote item isn't found. Verify the item and try again.")
43  );
44  }
45 
46  $item = $this->cart->updateItem($id, new \Magento\Framework\DataObject($params));
47  if (is_string($item)) {
48  throw new \Magento\Framework\Exception\LocalizedException(__($item));
49  }
50  if ($item->getHasError()) {
51  throw new \Magento\Framework\Exception\LocalizedException(__($item->getMessage()));
52  }
53 
54  $related = $this->getRequest()->getParam('related_product');
55  if (!empty($related)) {
56  $this->cart->addProductsByIds(explode(',', $related));
57  }
58 
59  $this->cart->save();
60 
61  $this->_eventManager->dispatch(
62  'checkout_cart_update_item_complete',
63  ['item' => $item, 'request' => $this->getRequest(), 'response' => $this->getResponse()]
64  );
65  if (!$this->_checkoutSession->getNoCartRedirect(true)) {
66  if (!$this->cart->getQuote()->getHasError()) {
67  $message = __(
68  '%1 was updated in your shopping cart.',
69  $this->_objectManager->get(\Magento\Framework\Escaper::class)
70  ->escapeHtml($item->getProduct()->getName())
71  );
72  $this->messageManager->addSuccessMessage($message);
73  }
74  return $this->_goBack($this->_url->getUrl('checkout/cart'));
75  }
76  } catch (\Magento\Framework\Exception\LocalizedException $e) {
77  if ($this->_checkoutSession->getUseNotice(true)) {
78  $this->messageManager->addNoticeMessage($e->getMessage());
79  } else {
80  $messages = array_unique(explode("\n", $e->getMessage()));
81  foreach ($messages as $message) {
82  $this->messageManager->addErrorMessage($message);
83  }
84  }
85 
86  $url = $this->_checkoutSession->getRedirectUrl(true);
87  if ($url) {
88  return $this->resultRedirectFactory->create()->setUrl($url);
89  } else {
90  $cartUrl = $this->_objectManager->get(\Magento\Checkout\Helper\Cart::class)->getCartUrl();
91  return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl($cartUrl));
92  }
93  } catch (\Exception $e) {
94  $this->messageManager->addExceptionMessage($e, __('We can\'t update the item right now.'));
95  $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
96  return $this->_goBack();
97  }
98  return $this->resultRedirectFactory->create()->setPath('*/*');
99  }
100 }
_redirect($path, $arguments=[])
Definition: Action.php:167
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$message
$quoteItem
Definition: quote.php:38
_goBack($backUrl=null)
Definition: Cart.php:73
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18