Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Add.php
Go to the documentation of this file.
1 <?php
7 
14 
19 {
23  protected $wishlistProvider;
24 
28  protected $_customerSession;
29 
33  protected $productRepository;
34 
38  protected $formKeyValidator;
39 
47  public function __construct(
48  Action\Context $context,
49  \Magento\Customer\Model\Session $customerSession,
53  ) {
54  $this->_customerSession = $customerSession;
55  $this->wishlistProvider = $wishlistProvider;
56  $this->productRepository = $productRepository;
57  $this->formKeyValidator = $formKeyValidator;
58  parent::__construct($context);
59  }
60 
70  public function execute()
71  {
73  $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
74  if (!$this->formKeyValidator->validate($this->getRequest())) {
75  return $resultRedirect->setPath('*/');
76  }
77 
78  $wishlist = $this->wishlistProvider->getWishlist();
79  if (!$wishlist) {
80  throw new NotFoundException(__('Page not found.'));
81  }
82 
84 
85  $requestParams = $this->getRequest()->getParams();
86 
87  if ($session->getBeforeWishlistRequest()) {
88  $requestParams = $session->getBeforeWishlistRequest();
89  $session->unsBeforeWishlistRequest();
90  }
91 
92  $productId = isset($requestParams['product']) ? (int)$requestParams['product'] : null;
93  if (!$productId) {
94  $resultRedirect->setPath('*/');
95  return $resultRedirect;
96  }
97 
98  try {
99  $product = $this->productRepository->getById($productId);
100  } catch (NoSuchEntityException $e) {
101  $product = null;
102  }
103 
104  if (!$product || !$product->isVisibleInCatalog()) {
105  $this->messageManager->addErrorMessage(__('We can\'t specify a product.'));
106  $resultRedirect->setPath('*/');
107  return $resultRedirect;
108  }
109 
110  try {
111  $buyRequest = new \Magento\Framework\DataObject($requestParams);
112 
113  $result = $wishlist->addNewItem($product, $buyRequest);
114  if (is_string($result)) {
115  throw new \Magento\Framework\Exception\LocalizedException(__($result));
116  }
117  if ($wishlist->isObjectNew()) {
118  $wishlist->save();
119  }
120  $this->_eventManager->dispatch(
121  'wishlist_add_product',
122  ['wishlist' => $wishlist, 'product' => $product, 'item' => $result]
123  );
124 
125  $referer = $session->getBeforeWishlistUrl();
126  if ($referer) {
127  $session->setBeforeWishlistUrl(null);
128  } else {
129  $referer = $this->_redirect->getRefererUrl();
130  }
131 
132  $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
133 
134  $this->messageManager->addComplexSuccessMessage(
135  'addProductSuccessMessage',
136  [
137  'product_name' => $product->getName(),
138  'referer' => $referer
139  ]
140  );
141  } catch (\Magento\Framework\Exception\LocalizedException $e) {
142  $this->messageManager->addErrorMessage(
143  __('We can\'t add the item to Wish List right now: %1.', $e->getMessage())
144  );
145  } catch (\Exception $e) {
146  $this->messageManager->addExceptionMessage(
147  $e,
148  __('We can\'t add the item to Wish List right now.')
149  );
150  }
151 
152  $resultRedirect->setPath('*', ['wishlist_id' => $wishlist->getId()]);
153  return $resultRedirect;
154  }
155 }
__()
Definition: __.php:13
$wishlist
Definition: wishlist.php:10
__construct(Action\Context $context, \Magento\Customer\Model\Session $customerSession, \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider, ProductRepositoryInterface $productRepository, Validator $formKeyValidator)
Definition: Add.php:47
foreach($product->getExtensionAttributes() ->getBundleProductOptions() as $option) $buyRequest