Exemple #1
0
 /**
  * Add shared wishlist item to shopping cart
  *
  * If Product has required options - redirect
  * to product view page with message about needed defined required options
  *
  * @return \Magento\Framework\Controller\Result\Redirect
  */
 public function execute()
 {
     $itemId = (int) $this->getRequest()->getParam('item');
     /* @var $item Item */
     $item = $this->itemFactory->create()->load($itemId);
     $redirectUrl = $this->_redirect->getRefererUrl();
     try {
         /** @var OptionCollection $options */
         $options = $this->optionFactory->create()->getCollection()->addItemFilter([$itemId]);
         $item->setOptions($options->getOptionsByItem($itemId));
         $item->addToCart($this->cart);
         $this->cart->save();
         if (!$this->cart->getQuote()->getHasError()) {
             $message = __('You added %1 to your shopping cart.', $this->escaper->escapeHtml($item->getProduct()->getName()));
             $this->messageManager->addSuccess($message);
         }
         if ($this->cartHelper->getShouldRedirectToCart()) {
             $redirectUrl = $this->cartHelper->getCartUrl();
         }
     } catch (ProductException $e) {
         $this->messageManager->addError(__('This product(s) is out of stock.'));
     } catch (LocalizedException $e) {
         $this->messageManager->addNotice($e->getMessage());
         $redirectUrl = $item->getProductUrl();
     } catch (\Exception $e) {
         $this->messageManager->addException($e, __('We can\'t add the item to the cart right now.'));
     }
     /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
     $resultRedirect->setUrl($redirectUrl);
     return $resultRedirect;
 }
Exemple #2
0
 /**
  * Add wishlist item to shopping cart and remove from wishlist
  *
  * If Product has required options - item removed from wishlist and redirect
  * to product view page with message about needed defined required options
  *
  * @return ResponseInterface
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function execute()
 {
     $itemId = (int) $this->getRequest()->getParam('item');
     /* @var $item \Magento\Wishlist\Model\Item */
     $item = $this->itemFactory->create()->load($itemId);
     if (!$item->getId()) {
         return $this->_redirect('*/*');
     }
     $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
     if (!$wishlist) {
         return $this->_redirect('*/*');
     }
     // Set qty
     $qty = $this->getRequest()->getParam('qty');
     if (is_array($qty)) {
         if (isset($qty[$itemId])) {
             $qty = $qty[$itemId];
         } else {
             $qty = 1;
         }
     }
     $qty = $this->quantityProcessor->process($qty);
     if ($qty) {
         $item->setQty($qty);
     }
     $redirectUrl = $this->_url->getUrl('*/*');
     $configureUrl = $this->_url->getUrl('*/*/configure/', ['id' => $item->getId(), 'product_id' => $item->getProductId()]);
     try {
         /** @var \Magento\Wishlist\Model\Resource\Item\Option\Collection $options */
         $options = $this->optionFactory->create()->getCollection()->addItemFilter([$itemId]);
         $item->setOptions($options->getOptionsByItem($itemId));
         $buyRequest = $this->productHelper->addParamsToBuyRequest($this->getRequest()->getParams(), ['current_config' => $item->getBuyRequest()]);
         $item->mergeBuyRequest($buyRequest);
         $item->addToCart($this->cart, true);
         $this->cart->save()->getQuote()->collectTotals();
         $wishlist->save();
         if (!$this->cart->getQuote()->getHasError()) {
             $message = __('You added %1 to your shopping cart.', $this->escaper->escapeHtml($item->getProduct()->getName()));
             $this->messageManager->addSuccess($message);
         }
         if ($this->cart->getShouldRedirectToCart()) {
             $redirectUrl = $this->cart->getCartUrl();
         } else {
             $refererUrl = $this->_redirect->getRefererUrl();
             if ($refererUrl && $refererUrl != $configureUrl) {
                 $redirectUrl = $refererUrl;
             }
         }
     } catch (ProductException $e) {
         $this->messageManager->addError(__('This product(s) is out of stock.'));
     } catch (\Magento\Framework\Exception\LocalizedException $e) {
         $this->messageManager->addNotice($e->getMessage());
         $redirectUrl = $configureUrl;
     } catch (\Exception $e) {
         $this->messageManager->addException($e, __('Cannot add item to shopping cart'));
     }
     $this->helper->calculate();
     return $this->getResponse()->setRedirect($redirectUrl);
 }
Exemple #3
0
 /**
  * Add option to item
  *
  * @param   Option|\Magento\Framework\Object|array $option
  * @return  $this
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function addOption($option)
 {
     if (is_array($option)) {
         $option = $this->_wishlistOptFactory->create()->setData($option)->setItem($this);
     } elseif ($option instanceof Option) {
         $option->setItem($this);
     } elseif ($option instanceof \Magento\Framework\Object) {
         $option = $this->_wishlistOptFactory->create()->setData($option->getData())->setProduct($option->getProduct())->setItem($this);
     } else {
         throw new \Magento\Framework\Exception\LocalizedException(__('Invalid item option format.'));
     }
     $exOption = $this->getOptionByCode($option->getCode());
     if ($exOption) {
         $exOption->addData($option->getData());
     } else {
         $this->_addOptionCode($option);
         $this->_options[] = $option;
     }
     return $this;
 }