Cart.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Controller\Shared;
  7. use Magento\Catalog\Model\Product\Exception as ProductException;
  8. use Magento\Checkout\Helper\Cart as CartHelper;
  9. use Magento\Checkout\Model\Cart as CustomerCart;
  10. use Magento\Framework\App\Action\Context as ActionContext;
  11. use Magento\Framework\Controller\ResultFactory;
  12. use Magento\Framework\Escaper;
  13. use Magento\Framework\Exception\LocalizedException;
  14. use Magento\Wishlist\Model\Item;
  15. use Magento\Wishlist\Model\Item\OptionFactory;
  16. use Magento\Wishlist\Model\ItemFactory;
  17. use Magento\Wishlist\Model\ResourceModel\Item\Option\Collection as OptionCollection;
  18. /**
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class Cart extends \Magento\Framework\App\Action\Action
  22. {
  23. /**
  24. * @var CustomerCart
  25. */
  26. protected $cart;
  27. /**
  28. * @var OptionFactory
  29. */
  30. protected $optionFactory;
  31. /**
  32. * @var ItemFactory
  33. */
  34. protected $itemFactory;
  35. /**
  36. * @var CartHelper
  37. */
  38. protected $cartHelper;
  39. /**
  40. * @var Escaper
  41. */
  42. protected $escaper;
  43. /**
  44. * @param ActionContext $context
  45. * @param CustomerCart $cart
  46. * @param OptionFactory $optionFactory
  47. * @param ItemFactory $itemFactory
  48. * @param CartHelper $cartHelper
  49. * @param Escaper $escaper
  50. */
  51. public function __construct(
  52. ActionContext $context,
  53. CustomerCart $cart,
  54. OptionFactory $optionFactory,
  55. ItemFactory $itemFactory,
  56. CartHelper $cartHelper,
  57. Escaper $escaper
  58. ) {
  59. $this->cart = $cart;
  60. $this->optionFactory = $optionFactory;
  61. $this->itemFactory = $itemFactory;
  62. $this->cartHelper = $cartHelper;
  63. $this->escaper = $escaper;
  64. parent::__construct($context);
  65. }
  66. /**
  67. * Add shared wishlist item to shopping cart
  68. *
  69. * If Product has required options - redirect
  70. * to product view page with message about needed defined required options
  71. *
  72. * @return \Magento\Framework\Controller\Result\Redirect
  73. */
  74. public function execute()
  75. {
  76. $itemId = (int)$this->getRequest()->getParam('item');
  77. /* @var $item Item */
  78. $item = $this->itemFactory->create()
  79. ->load($itemId);
  80. $redirectUrl = $this->_redirect->getRefererUrl();
  81. try {
  82. /** @var OptionCollection $options */
  83. $options = $this->optionFactory->create()
  84. ->getCollection()->addItemFilter([$itemId]);
  85. $item->setOptions($options->getOptionsByItem($itemId));
  86. $item->addToCart($this->cart);
  87. $this->cart->save();
  88. if (!$this->cart->getQuote()->getHasError()) {
  89. $message = __(
  90. 'You added %1 to your shopping cart.',
  91. $this->escaper->escapeHtml($item->getProduct()->getName())
  92. );
  93. $this->messageManager->addSuccess($message);
  94. }
  95. if ($this->cartHelper->getShouldRedirectToCart()) {
  96. $redirectUrl = $this->cartHelper->getCartUrl();
  97. }
  98. } catch (ProductException $e) {
  99. $this->messageManager->addError(__('This product(s) is out of stock.'));
  100. } catch (LocalizedException $e) {
  101. $this->messageManager->addNotice($e->getMessage());
  102. $redirectUrl = $item->getProductUrl();
  103. } catch (\Exception $e) {
  104. $this->messageManager->addException($e, __('We can\'t add the item to the cart right now.'));
  105. }
  106. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  107. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  108. $resultRedirect->setUrl($redirectUrl);
  109. return $resultRedirect;
  110. }
  111. }