/** * 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; }
/** * 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()); }
/** * 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); }
/** * {@inheritdoc} */ public function save() { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'save'); if (!$pluginInfo) { return parent::save(); } else { return $this->___callPlugins('save', func_get_args(), $pluginInfo); } }
/** * Remove quote item * * @param int $itemId * @return $this */ public function removeQuoteItem($itemId) { $this->cart->removeItem($itemId); $this->cart->save(); return $this; }
/** * @return \Magento\Framework\View\Result\PageFactory */ public function execute() { $pId = $this->getRequest()->getParam('pid'); //productId $sdcnt = $this->getRequest()->getParam('sdcnt'); //shoppree discount /* $resultPage = $this->resultPageFactory->create(); $resultPage->getConfig()->getTitle()->prepend(__('Shoppree Offers')); return $resultPage; $layout = $this->_view->getLayout(); $block = $layout->createBlock('Shoppree\Offers\Block\OfferBlock'); echo $block->getCurrentStoreName(); echo '<br>'; echo $block->getProductName('1'); */ $layout = $this->_view->getLayout(); $block = $layout->createBlock('Shoppree\\Offers\\Block\\OfferBlock'); try { if (!empty($pId)) { $params = array(); $params['qty'] = '1'; //product quantity /*get product id*/ //$pId = '1';//productId $_product = $this->product->load($pId); if ($_product) { $quote = $this->cart->getQuote(); //Create object of quote $_product->setPrice('0.00'); //$_product->setFinalPrice('0.00'); $this->cart->addProduct($_product, $params); $this->cart->setItemsQty(); //$quote->setGrandTotal($this->cart->getQuote()->getGrandTotal() - 50.00); //$this->cart->setQuote($quote); $this->cart->saveQuote(); $this->_checkoutSession->setCartWasUpdated(false); $this->cart->save(); $this->_checkoutSession->unsShoppreeDiscount(); $this->messageManager->addSuccess(__('Add to cart successfully.')); } } else { if (!empty($sdcnt)) { /*sets shoppree discount*/ $this->_checkoutSession->setShoppreeDiscount($sdcnt); /*$this->getResponse()->setRedirect('/magento-prototype/checkout/cart');*/ } } //$this->messageManager->addSuccess(__('Add to cart successfully.').'--'.$this->_checkoutSession->getQuote()->getSubtotal().'--'.$this->_checkoutSession->getQuote()->getGrandTotal().'--'.$this->cart->getQuote()->getGrandTotal().'--'.$this->cart->getQuote()->getSubtotal()); //$this->messageManager->addSuccess(__('Add to cart successfully.').$block->getGrandTotal().'--'.$this->cart->getQuote()->getGrandTotal()); /* $this->messageManager->addSuccess(__('Add to cart successfully.').$this->cart->getItemsCount().'--'.$this->cart->getItemsQty()); $this->getResponse()->setRedirect('/magento-prototype/checkout/cart'); */ } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->messageManager->addException($e, __('%1', $e->getMessage())); } catch (\Exception $e) { $this->messageManager->addException($e, __('error.')); } }