/**
  * Check move quote item to wishlist request
  *
  * @param   Observer $observer
  * @return  $this
  */
 public function execute(Observer $observer)
 {
     $cart = $observer->getEvent()->getCart();
     $data = $observer->getEvent()->getInfo()->toArray();
     $productIds = [];
     $wishlist = $this->getWishlist($cart->getQuote()->getCustomerId());
     if (!$wishlist) {
         return $this;
     }
     /**
      * Collect product ids marked for move to wishlist
      */
     foreach ($data as $itemId => $itemInfo) {
         if (!empty($itemInfo['wishlist']) && ($item = $cart->getQuote()->getItemById($itemId))) {
             $productId = $item->getProductId();
             $buyRequest = $item->getBuyRequest();
             if (array_key_exists('qty', $itemInfo) && is_numeric($itemInfo['qty'])) {
                 $buyRequest->setQty($itemInfo['qty']);
             }
             $wishlist->addNewItem($productId, $buyRequest);
             $productIds[] = $productId;
             $cart->getQuote()->removeItem($itemId);
         }
     }
     if (count($productIds)) {
         $wishlist->save();
         $this->wishlistData->calculate();
     }
     return $this;
 }
Exemplo n.º 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());
 }
Exemplo n.º 3
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);
 }
Exemplo n.º 4
0
 /**
  * @param Observer $observer
  * @return void
  */
 public function execute(Observer $observer)
 {
     $this->wishlistData->calculate();
 }
Exemplo n.º 5
0
 /**
  * Customer login processing
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return $this
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function customerLogin(\Magento\Framework\Event\Observer $observer)
 {
     $this->_wishlistData->calculate();
     return $this;
 }
Exemplo n.º 6
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;
 }
Exemplo n.º 7
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 = [];
     $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;
 }