Cart.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Controller\Index;
  7. use Magento\Framework\App\Action;
  8. use Magento\Catalog\Model\Product\Exception as ProductException;
  9. use Magento\Framework\Controller\ResultFactory;
  10. /**
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class Cart extends \Magento\Wishlist\Controller\AbstractIndex
  14. {
  15. /**
  16. * @var \Magento\Wishlist\Controller\WishlistProviderInterface
  17. */
  18. protected $wishlistProvider;
  19. /**
  20. * @var \Magento\Wishlist\Model\LocaleQuantityProcessor
  21. */
  22. protected $quantityProcessor;
  23. /**
  24. * @var \Magento\Wishlist\Model\ItemFactory
  25. */
  26. protected $itemFactory;
  27. /**
  28. * @var \Magento\Checkout\Model\Cart
  29. */
  30. protected $cart;
  31. /**
  32. * @var \Magento\Checkout\Helper\Cart
  33. */
  34. protected $cartHelper;
  35. /**
  36. * @var \Magento\Wishlist\Model\Item\OptionFactory
  37. */
  38. private $optionFactory;
  39. /**
  40. * @var \Magento\Catalog\Helper\Product
  41. */
  42. protected $productHelper;
  43. /**
  44. * @var \Magento\Framework\Escaper
  45. */
  46. protected $escaper;
  47. /**
  48. * @var \Magento\Wishlist\Helper\Data
  49. */
  50. protected $helper;
  51. /**
  52. * @var \Magento\Framework\Data\Form\FormKey\Validator
  53. */
  54. protected $formKeyValidator;
  55. /**
  56. * @param Action\Context $context
  57. * @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
  58. * @param \Magento\Wishlist\Model\LocaleQuantityProcessor $quantityProcessor
  59. * @param \Magento\Wishlist\Model\ItemFactory $itemFactory
  60. * @param \Magento\Checkout\Model\Cart $cart
  61. * @param \Magento\Wishlist\Model\Item\OptionFactory $optionFactory
  62. * @param \Magento\Catalog\Helper\Product $productHelper
  63. * @param \Magento\Framework\Escaper $escaper
  64. * @param \Magento\Wishlist\Helper\Data $helper
  65. * @param \Magento\Checkout\Helper\Cart $cartHelper
  66. * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
  67. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  68. */
  69. public function __construct(
  70. Action\Context $context,
  71. \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider,
  72. \Magento\Wishlist\Model\LocaleQuantityProcessor $quantityProcessor,
  73. \Magento\Wishlist\Model\ItemFactory $itemFactory,
  74. \Magento\Checkout\Model\Cart $cart,
  75. \Magento\Wishlist\Model\Item\OptionFactory $optionFactory,
  76. \Magento\Catalog\Helper\Product $productHelper,
  77. \Magento\Framework\Escaper $escaper,
  78. \Magento\Wishlist\Helper\Data $helper,
  79. \Magento\Checkout\Helper\Cart $cartHelper,
  80. \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
  81. ) {
  82. $this->wishlistProvider = $wishlistProvider;
  83. $this->quantityProcessor = $quantityProcessor;
  84. $this->itemFactory = $itemFactory;
  85. $this->cart = $cart;
  86. $this->optionFactory = $optionFactory;
  87. $this->productHelper = $productHelper;
  88. $this->escaper = $escaper;
  89. $this->helper = $helper;
  90. $this->cartHelper = $cartHelper;
  91. $this->formKeyValidator = $formKeyValidator;
  92. parent::__construct($context);
  93. }
  94. /**
  95. * Add wishlist item to shopping cart and remove from wishlist
  96. *
  97. * If Product has required options - item removed from wishlist and redirect
  98. * to product view page with message about needed defined required options
  99. *
  100. * @return \Magento\Framework\Controller\ResultInterface
  101. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  102. * @SuppressWarnings(PHPMD.NPathComplexity)
  103. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  104. */
  105. public function execute()
  106. {
  107. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  108. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  109. if (!$this->formKeyValidator->validate($this->getRequest())) {
  110. return $resultRedirect->setPath('*/*/');
  111. }
  112. $itemId = (int)$this->getRequest()->getParam('item');
  113. /* @var $item \Magento\Wishlist\Model\Item */
  114. $item = $this->itemFactory->create()->load($itemId);
  115. if (!$item->getId()) {
  116. $resultRedirect->setPath('*/*');
  117. return $resultRedirect;
  118. }
  119. $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
  120. if (!$wishlist) {
  121. $resultRedirect->setPath('*/*');
  122. return $resultRedirect;
  123. }
  124. // Set qty
  125. $qty = $this->getRequest()->getParam('qty');
  126. $postQty = $this->getRequest()->getPostValue('qty');
  127. if ($postQty !== null && $qty !== $postQty) {
  128. $qty = $postQty;
  129. }
  130. if (is_array($qty)) {
  131. if (isset($qty[$itemId])) {
  132. $qty = $qty[$itemId];
  133. } else {
  134. $qty = 1;
  135. }
  136. }
  137. $qty = $this->quantityProcessor->process($qty);
  138. if ($qty) {
  139. $item->setQty($qty);
  140. }
  141. $redirectUrl = $this->_url->getUrl('*/*');
  142. $configureUrl = $this->_url->getUrl(
  143. '*/*/configure/',
  144. [
  145. 'id' => $item->getId(),
  146. 'product_id' => $item->getProductId(),
  147. ]
  148. );
  149. try {
  150. /** @var \Magento\Wishlist\Model\ResourceModel\Item\Option\Collection $options */
  151. $options = $this->optionFactory->create()->getCollection()->addItemFilter([$itemId]);
  152. $item->setOptions($options->getOptionsByItem($itemId));
  153. $buyRequest = $this->productHelper->addParamsToBuyRequest(
  154. $this->getRequest()->getParams(),
  155. ['current_config' => $item->getBuyRequest()]
  156. );
  157. $item->mergeBuyRequest($buyRequest);
  158. $item->addToCart($this->cart, true);
  159. $this->cart->save()->getQuote()->collectTotals();
  160. $wishlist->save();
  161. if (!$this->cart->getQuote()->getHasError()) {
  162. $message = __(
  163. 'You added %1 to your shopping cart.',
  164. $this->escaper->escapeHtml($item->getProduct()->getName())
  165. );
  166. $this->messageManager->addSuccess($message);
  167. }
  168. if ($this->cartHelper->getShouldRedirectToCart()) {
  169. $redirectUrl = $this->cartHelper->getCartUrl();
  170. } else {
  171. $refererUrl = $this->_redirect->getRefererUrl();
  172. if ($refererUrl && $refererUrl != $configureUrl) {
  173. $redirectUrl = $refererUrl;
  174. }
  175. }
  176. } catch (ProductException $e) {
  177. $this->messageManager->addError(__('This product(s) is out of stock.'));
  178. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  179. $this->messageManager->addNotice($e->getMessage());
  180. $redirectUrl = $configureUrl;
  181. } catch (\Exception $e) {
  182. $this->messageManager->addException($e, __('We can\'t add the item to the cart right now.'));
  183. }
  184. $this->helper->calculate();
  185. if ($this->getRequest()->isAjax()) {
  186. /** @var \Magento\Framework\Controller\Result\Json $resultJson */
  187. $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
  188. $resultJson->setData(['backUrl' => $redirectUrl]);
  189. return $resultJson;
  190. }
  191. $resultRedirect->setUrl($redirectUrl);
  192. return $resultRedirect;
  193. }
  194. }