Example #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 (LocalizedException $e) {
         if ($e->getCode() == Item::EXCEPTION_CODE_NOT_SALABLE) {
             $this->messageManager->addError(__('This product(s) is out of stock.'));
         } else {
             $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;
 }
Example #2
0
 /**
  * Add cart item to wishlist and remove from cart
  *
  * @return \Magento\Framework\Controller\Result\Redirect
  * @throws NotFoundException
  * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  */
 public function execute()
 {
     $wishlist = $this->wishlistProvider->getWishlist();
     if (!$wishlist) {
         throw new NotFoundException(__('Page not found.'));
     }
     try {
         $itemId = (int) $this->getRequest()->getParam('item');
         $item = $this->cart->getQuote()->getItemById($itemId);
         if (!$item) {
             throw new LocalizedException(__('The requested cart item doesn\'t exist.'));
         }
         $productId = $item->getProductId();
         $buyRequest = $item->getBuyRequest();
         $wishlist->addNewItem($productId, $buyRequest);
         $this->cart->getQuote()->removeItem($itemId);
         $this->cart->save();
         $this->wishlistHelper->calculate();
         $wishlist->save();
         $this->messageManager->addSuccessMessage(__("%1 has been moved to your wish list.", $this->escaper->escapeHtml($item->getProduct()->getName())));
     } catch (LocalizedException $e) {
         $this->messageManager->addErrorMessage($e->getMessage());
     } catch (\Exception $e) {
         $this->messageManager->addExceptionMessage($e, __('We can\'t move the item to the wish list.'));
     }
     /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
     return $resultRedirect->setUrl($this->cartHelper->getCartUrl());
 }
Example #3
0
 public function testGetCartUrl()
 {
     $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('checkout/cart', array());
     $this->helper->getCartUrl();
 }
Example #4
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 \Magento\Framework\Controller\ResultInterface
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function execute()
 {
     $itemId = (int) $this->getRequest()->getParam('item');
     /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
     /* @var $item \Magento\Wishlist\Model\Item */
     $item = $this->itemFactory->create()->load($itemId);
     if (!$item->getId()) {
         $resultRedirect->setPath('*/*');
         return $resultRedirect;
     }
     $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
     if (!$wishlist) {
         $resultRedirect->setPath('*/*');
         return $resultRedirect;
     }
     // 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->cartHelper->getShouldRedirectToCart()) {
             $redirectUrl = $this->cartHelper->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, __('We can\'t add the item to the cart right now.'));
     }
     $this->helper->calculate();
     if ($this->getRequest()->isAjax()) {
         /** @var \Magento\Framework\Controller\Result\Json $resultJson */
         $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
         $resultJson->setData(['backUrl' => $redirectUrl]);
         return $resultJson;
     }
     $resultRedirect->setUrl($redirectUrl);
     return $resultRedirect;
 }
Example #5
0
 /**
  * Move all wishlist item to cart
  *
  * @param Wishlist $wishlist
  * @param array $qtys
  * @return string
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function moveAllToCart(Wishlist $wishlist, $qtys)
 {
     $isOwner = $wishlist->isOwner($this->customerSession->getCustomerId());
     $messages = [];
     $addedItems = [];
     $notSalable = [];
     $hasOptions = [];
     $cart = $this->cart;
     $collection = $wishlist->getItemCollection()->setVisibilityFilter();
     foreach ($collection as $item) {
         /** @var \Magento\Wishlist\Model\Item */
         try {
             $disableAddToCart = $item->getProduct()->getDisableAddToCart();
             $item->unsProduct();
             // Set qty
             if (isset($qtys[$item->getId()])) {
                 $qty = $this->quantityProcessor->process($qtys[$item->getId()]);
                 if ($qty) {
                     $item->setQty($qty);
                 }
             }
             $item->getProduct()->setDisableAddToCart($disableAddToCart);
             // Add to cart
             if ($item->addToCart($cart, $isOwner)) {
                 $addedItems[] = $item->getProduct();
             }
         } catch (\Magento\Framework\Exception\LocalizedException $e) {
             if ($e->getCode() == \Magento\Wishlist\Model\Item::EXCEPTION_CODE_NOT_SALABLE) {
                 $notSalable[] = $item;
             } elseif ($e->getCode() == \Magento\Wishlist\Model\Item::EXCEPTION_CODE_HAS_REQUIRED_OPTIONS) {
                 $hasOptions[] = $item;
             } else {
                 $messages[] = __('%1 for "%2".', trim($e->getMessage(), '.'), $item->getProduct()->getName());
             }
             $cartItem = $cart->getQuote()->getItemByProduct($item->getProduct());
             if ($cartItem) {
                 $cart->getQuote()->deleteItem($cartItem);
             }
         } catch (\Exception $e) {
             $this->logger->critical($e);
             $messages[] = __('We cannot add this item to your shopping cart.');
         }
     }
     if ($isOwner) {
         $indexUrl = $this->helper->getListUrl($wishlist->getId());
     } else {
         $indexUrl = $this->urlBuilder->getUrl('wishlist/shared', ['code' => $wishlist->getSharingCode()]);
     }
     if ($this->cartHelper->getShouldRedirectToCart()) {
         $redirectUrl = $this->cartHelper->getCartUrl();
     } elseif ($this->redirector->getRefererUrl()) {
         $redirectUrl = $this->redirector->getRefererUrl();
     } else {
         $redirectUrl = $indexUrl;
     }
     if ($notSalable) {
         $products = [];
         foreach ($notSalable as $item) {
             $products[] = '"' . $item->getProduct()->getName() . '"';
         }
         $messages[] = __('We couldn\'t add the following product(s) to the shopping cart: %1.', join(', ', $products));
     }
     if ($hasOptions) {
         $products = [];
         foreach ($hasOptions as $item) {
             $products[] = '"' . $item->getProduct()->getName() . '"';
         }
         $messages[] = __('Product(s) %1 have required options. Each product can only be added individually.', join(', ', $products));
     }
     if ($messages) {
         $isMessageSole = count($messages) == 1;
         if ($isMessageSole && count($hasOptions) == 1) {
             $item = $hasOptions[0];
             if ($isOwner) {
                 $item->delete();
             }
             $redirectUrl = $item->getProductUrl();
         } else {
             foreach ($messages as $message) {
                 $this->messageManager->addError($message);
             }
             $redirectUrl = $indexUrl;
         }
     }
     if ($addedItems) {
         // save wishlist model for setting date of last update
         try {
             $wishlist->save();
         } catch (\Exception $e) {
             $this->messageManager->addError(__('We can\'t update wish list.'));
             $redirectUrl = $indexUrl;
         }
         $products = [];
         foreach ($addedItems as $product) {
             $products[] = '"' . $product->getName() . '"';
         }
         $this->messageManager->addSuccess(__('%1 product(s) have been added to shopping cart: %2.', count($addedItems), join(', ', $products)));
         // save cart and collect totals
         $cart->save()->getQuote()->collectTotals();
     }
     $this->helper->calculate();
     return $redirectUrl;
 }
 /**
  * Move all wishlist item to cart
  *
  * @param Wishlist $wishlist
  * @param array $qtys
  * @return string
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function moveAllToCart(Wishlist $wishlist, $qtys)
 {
     $isOwner = $wishlist->isOwner($this->customerSession->getCustomerId());
     $messages = [];
     $addedProducts = [];
     $notSalable = [];
     $cart = $this->cart;
     $collection = $wishlist->getItemCollection()->setVisibilityFilter();
     foreach ($collection as $item) {
         /** @var $item \Magento\Wishlist\Model\Item */
         try {
             $disableAddToCart = $item->getProduct()->getDisableAddToCart();
             $item->unsProduct();
             // Set qty
             if (isset($qtys[$item->getId()])) {
                 $qty = $this->quantityProcessor->process($qtys[$item->getId()]);
                 if ($qty) {
                     $item->setQty($qty);
                 }
             }
             $item->getProduct()->setDisableAddToCart($disableAddToCart);
             // Add to cart
             if ($item->addToCart($cart, $isOwner)) {
                 $addedProducts[] = $item->getProduct();
             }
         } catch (LocalizedException $e) {
             if ($e instanceof ProductException) {
                 $notSalable[] = $item;
             } else {
                 $messages[] = __('%1 for "%2".', trim($e->getMessage(), '.'), $item->getProduct()->getName());
             }
             $cartItem = $cart->getQuote()->getItemByProduct($item->getProduct());
             if ($cartItem) {
                 $cart->getQuote()->deleteItem($cartItem);
             }
         } catch (\Exception $e) {
             $this->logger->critical($e);
             $messages[] = __('We can\'t add this item to your shopping cart right now.');
         }
     }
     if ($isOwner) {
         $indexUrl = $this->helper->getListUrl($wishlist->getId());
     } else {
         $indexUrl = $this->urlBuilder->getUrl('wishlist/shared', ['code' => $wishlist->getSharingCode()]);
     }
     if ($this->cartHelper->getShouldRedirectToCart()) {
         $redirectUrl = $this->cartHelper->getCartUrl();
     } elseif ($this->redirector->getRefererUrl()) {
         $redirectUrl = $this->redirector->getRefererUrl();
     } else {
         $redirectUrl = $indexUrl;
     }
     if ($notSalable) {
         $products = [];
         foreach ($notSalable as $item) {
             $products[] = '"' . $item->getProduct()->getName() . '"';
         }
         $messages[] = __('We couldn\'t add the following product(s) to the shopping cart: %1.', join(', ', $products));
     }
     if ($messages) {
         foreach ($messages as $message) {
             $this->messageManager->addError($message);
         }
         $redirectUrl = $indexUrl;
     }
     if ($addedProducts) {
         // save wishlist model for setting date of last update
         try {
             $wishlist->save();
         } catch (\Exception $e) {
             $this->messageManager->addError(__('We can\'t update the Wish List right now.'));
             $redirectUrl = $indexUrl;
         }
         $products = [];
         foreach ($addedProducts as $product) {
             /** @var $product \Magento\Catalog\Model\Product */
             $products[] = '"' . $product->getName() . '"';
         }
         $this->messageManager->addSuccess(__('%1 product(s) have been added to shopping cart: %2.', count($addedProducts), join(', ', $products)));
         // save cart and collect totals
         $cart->save()->getQuote()->collectTotals();
     }
     $this->helper->calculate();
     return $redirectUrl;
 }