PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-checkout/Controller/Cart/UpdateItemOptions.php

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