UpdateItemOptions.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Checkout\Controller\Cart;
  8. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  9. class UpdateItemOptions extends \Magento\Checkout\Controller\Cart implements HttpPostActionInterface
  10. {
  11. /**
  12. * Update product configuration for a cart item
  13. *
  14. * @return \Magento\Framework\Controller\Result\Redirect
  15. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  16. * @SuppressWarnings(PHPMD.NPathComplexity)
  17. */
  18. public function execute()
  19. {
  20. $id = (int)$this->getRequest()->getParam('id');
  21. $params = $this->getRequest()->getParams();
  22. if (!isset($params['options'])) {
  23. $params['options'] = [];
  24. }
  25. try {
  26. if (isset($params['qty'])) {
  27. $filter = new \Zend_Filter_LocalizedToNormalized(
  28. ['locale' => $this->_objectManager->get(
  29. \Magento\Framework\Locale\ResolverInterface::class
  30. )->getLocale()]
  31. );
  32. $params['qty'] = $filter->filter($params['qty']);
  33. }
  34. $quoteItem = $this->cart->getQuote()->getItemById($id);
  35. if (!$quoteItem) {
  36. throw new \Magento\Framework\Exception\LocalizedException(
  37. __("The quote item isn't found. Verify the item and try again.")
  38. );
  39. }
  40. $item = $this->cart->updateItem($id, new \Magento\Framework\DataObject($params));
  41. if (is_string($item)) {
  42. throw new \Magento\Framework\Exception\LocalizedException(__($item));
  43. }
  44. if ($item->getHasError()) {
  45. throw new \Magento\Framework\Exception\LocalizedException(__($item->getMessage()));
  46. }
  47. $related = $this->getRequest()->getParam('related_product');
  48. if (!empty($related)) {
  49. $this->cart->addProductsByIds(explode(',', $related));
  50. }
  51. $this->cart->save();
  52. $this->_eventManager->dispatch(
  53. 'checkout_cart_update_item_complete',
  54. ['item' => $item, 'request' => $this->getRequest(), 'response' => $this->getResponse()]
  55. );
  56. if (!$this->_checkoutSession->getNoCartRedirect(true)) {
  57. if (!$this->cart->getQuote()->getHasError()) {
  58. $message = __(
  59. '%1 was updated in your shopping cart.',
  60. $this->_objectManager->get(\Magento\Framework\Escaper::class)
  61. ->escapeHtml($item->getProduct()->getName())
  62. );
  63. $this->messageManager->addSuccessMessage($message);
  64. }
  65. return $this->_goBack($this->_url->getUrl('checkout/cart'));
  66. }
  67. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  68. if ($this->_checkoutSession->getUseNotice(true)) {
  69. $this->messageManager->addNoticeMessage($e->getMessage());
  70. } else {
  71. $messages = array_unique(explode("\n", $e->getMessage()));
  72. foreach ($messages as $message) {
  73. $this->messageManager->addErrorMessage($message);
  74. }
  75. }
  76. $url = $this->_checkoutSession->getRedirectUrl(true);
  77. if ($url) {
  78. return $this->resultRedirectFactory->create()->setUrl($url);
  79. } else {
  80. $cartUrl = $this->_objectManager->get(\Magento\Checkout\Helper\Cart::class)->getCartUrl();
  81. return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl($cartUrl));
  82. }
  83. } catch (\Exception $e) {
  84. $this->messageManager->addExceptionMessage($e, __('We can\'t update the item right now.'));
  85. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
  86. return $this->_goBack();
  87. }
  88. return $this->resultRedirectFactory->create()->setPath('*/*');
  89. }
  90. }