45 private $optionFactory;
87 \
Magento\Wishlist\Model\
Item\OptionFactory $optionFactory,
98 $this->optionFactory = $optionFactory;
104 parent::__construct($context);
122 if (!$this->formKeyValidator->validate($this->getRequest())) {
123 return $resultRedirect->setPath(
'*/*/');
126 $itemId = (int)$this->getRequest()->getParam(
'item');
128 $item = $this->itemFactory->create()->load($itemId);
129 if (!
$item->getId()) {
130 $resultRedirect->setPath(
'*/*');
131 return $resultRedirect;
133 $wishlist = $this->wishlistProvider->getWishlist(
$item->getWishlistId());
135 $resultRedirect->setPath(
'*/*');
136 return $resultRedirect;
140 $qty = $this->getRequest()->getParam(
'qty');
141 $postQty = $this->getRequest()->getPostValue(
'qty');
142 if ($postQty !==
null && $qty !== $postQty) {
145 if (is_array($qty)) {
146 if (isset($qty[$itemId])) {
147 $qty = $qty[$itemId];
152 $qty = $this->quantityProcessor->process($qty);
157 $redirectUrl = $this->_url->getUrl(
'*/*');
158 $configureUrl = $this->_url->getUrl(
161 'id' =>
$item->getId(),
162 'product_id' =>
$item->getProductId(),
168 $options = $this->optionFactory->create()->getCollection()->addItemFilter([$itemId]);
171 $buyRequest = $this->productHelper->addParamsToBuyRequest(
172 $this->getRequest()->getParams(),
173 [
'current_config' =>
$item->getBuyRequest()]
177 $item->addToCart($this->cart,
true);
178 $this->cart->save()->getQuote()->collectTotals();
181 if (!$this->cart->getQuote()->getHasError()) {
183 'You added %1 to your shopping cart.',
184 $this->escaper->escapeHtml(
$item->getProduct()->getName())
186 $this->messageManager->addSuccess(
$message);
189 if ($this->cartHelper->getShouldRedirectToCart()) {
190 $redirectUrl = $this->cartHelper->getCartUrl();
192 $refererUrl = $this->_redirect->getRefererUrl();
193 if ($refererUrl && $refererUrl != $configureUrl) {
194 $redirectUrl = $refererUrl;
197 }
catch (ProductException $e) {
198 $this->messageManager->addError(
__(
'This product(s) is out of stock.'));
199 }
catch (\
Magento\Framework\Exception\LocalizedException $e) {
200 $this->messageManager->addNotice($e->getMessage());
201 $redirectUrl = $configureUrl;
202 }
catch (\Exception $e) {
203 $this->messageManager->addException($e,
__(
'We can\'t add the item to the cart right now.'));
206 $this->helper->calculate();
208 if ($this->getRequest()->isAjax()) {
211 $resultJson->setData([
'backUrl' => $redirectUrl]);
215 $resultRedirect->setUrl($redirectUrl);
216 return $resultRedirect;
__construct(Action\Context $context, \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider, \Magento\Wishlist\Model\LocaleQuantityProcessor $quantityProcessor, \Magento\Wishlist\Model\ItemFactory $itemFactory, \Magento\Checkout\Model\Cart $cart, \Magento\Wishlist\Model\Item\OptionFactory $optionFactory, \Magento\Catalog\Helper\Product $productHelper, \Magento\Framework\Escaper $escaper, \Magento\Wishlist\Helper\Data $helper, \Magento\Checkout\Helper\Cart $cartHelper, \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator)
foreach($product->getExtensionAttributes() ->getBundleProductOptions() as $option) $buyRequest