Add.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\Catalog\Api\ProductRepositoryInterface;
  8. use Magento\Framework\App\Action;
  9. use Magento\Framework\Data\Form\FormKey\Validator;
  10. use Magento\Framework\Exception\NotFoundException;
  11. use Magento\Framework\Exception\NoSuchEntityException;
  12. use Magento\Framework\Controller\ResultFactory;
  13. /**
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class Add extends \Magento\Wishlist\Controller\AbstractIndex
  17. {
  18. /**
  19. * @var \Magento\Wishlist\Controller\WishlistProviderInterface
  20. */
  21. protected $wishlistProvider;
  22. /**
  23. * @var \Magento\Customer\Model\Session
  24. */
  25. protected $_customerSession;
  26. /**
  27. * @var ProductRepositoryInterface
  28. */
  29. protected $productRepository;
  30. /**
  31. * @var Validator
  32. */
  33. protected $formKeyValidator;
  34. /**
  35. * @param Action\Context $context
  36. * @param \Magento\Customer\Model\Session $customerSession
  37. * @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
  38. * @param ProductRepositoryInterface $productRepository
  39. * @param Validator $formKeyValidator
  40. */
  41. public function __construct(
  42. Action\Context $context,
  43. \Magento\Customer\Model\Session $customerSession,
  44. \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider,
  45. ProductRepositoryInterface $productRepository,
  46. Validator $formKeyValidator
  47. ) {
  48. $this->_customerSession = $customerSession;
  49. $this->wishlistProvider = $wishlistProvider;
  50. $this->productRepository = $productRepository;
  51. $this->formKeyValidator = $formKeyValidator;
  52. parent::__construct($context);
  53. }
  54. /**
  55. * Adding new item
  56. *
  57. * @return \Magento\Framework\Controller\Result\Redirect
  58. * @throws NotFoundException
  59. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  60. * @SuppressWarnings(PHPMD.NPathComplexity)
  61. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  62. */
  63. public function execute()
  64. {
  65. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  66. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  67. if (!$this->formKeyValidator->validate($this->getRequest())) {
  68. return $resultRedirect->setPath('*/');
  69. }
  70. $wishlist = $this->wishlistProvider->getWishlist();
  71. if (!$wishlist) {
  72. throw new NotFoundException(__('Page not found.'));
  73. }
  74. $session = $this->_customerSession;
  75. $requestParams = $this->getRequest()->getParams();
  76. if ($session->getBeforeWishlistRequest()) {
  77. $requestParams = $session->getBeforeWishlistRequest();
  78. $session->unsBeforeWishlistRequest();
  79. }
  80. $productId = isset($requestParams['product']) ? (int)$requestParams['product'] : null;
  81. if (!$productId) {
  82. $resultRedirect->setPath('*/');
  83. return $resultRedirect;
  84. }
  85. try {
  86. $product = $this->productRepository->getById($productId);
  87. } catch (NoSuchEntityException $e) {
  88. $product = null;
  89. }
  90. if (!$product || !$product->isVisibleInCatalog()) {
  91. $this->messageManager->addErrorMessage(__('We can\'t specify a product.'));
  92. $resultRedirect->setPath('*/');
  93. return $resultRedirect;
  94. }
  95. try {
  96. $buyRequest = new \Magento\Framework\DataObject($requestParams);
  97. $result = $wishlist->addNewItem($product, $buyRequest);
  98. if (is_string($result)) {
  99. throw new \Magento\Framework\Exception\LocalizedException(__($result));
  100. }
  101. if ($wishlist->isObjectNew()) {
  102. $wishlist->save();
  103. }
  104. $this->_eventManager->dispatch(
  105. 'wishlist_add_product',
  106. ['wishlist' => $wishlist, 'product' => $product, 'item' => $result]
  107. );
  108. $referer = $session->getBeforeWishlistUrl();
  109. if ($referer) {
  110. $session->setBeforeWishlistUrl(null);
  111. } else {
  112. $referer = $this->_redirect->getRefererUrl();
  113. }
  114. $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
  115. $this->messageManager->addComplexSuccessMessage(
  116. 'addProductSuccessMessage',
  117. [
  118. 'product_name' => $product->getName(),
  119. 'referer' => $referer
  120. ]
  121. );
  122. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  123. $this->messageManager->addErrorMessage(
  124. __('We can\'t add the item to Wish List right now: %1.', $e->getMessage())
  125. );
  126. } catch (\Exception $e) {
  127. $this->messageManager->addExceptionMessage(
  128. $e,
  129. __('We can\'t add the item to Wish List right now.')
  130. );
  131. }
  132. $resultRedirect->setPath('*', ['wishlist_id' => $wishlist->getId()]);
  133. return $resultRedirect;
  134. }
  135. }