123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Wishlist\Controller\Index;
- use Magento\Framework\App\Action;
- use Magento\Catalog\Model\Product\Exception as ProductException;
- use Magento\Framework\Controller\ResultFactory;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Cart extends \Magento\Wishlist\Controller\AbstractIndex
- {
- /**
- * @var \Magento\Wishlist\Controller\WishlistProviderInterface
- */
- protected $wishlistProvider;
- /**
- * @var \Magento\Wishlist\Model\LocaleQuantityProcessor
- */
- protected $quantityProcessor;
- /**
- * @var \Magento\Wishlist\Model\ItemFactory
- */
- protected $itemFactory;
- /**
- * @var \Magento\Checkout\Model\Cart
- */
- protected $cart;
- /**
- * @var \Magento\Checkout\Helper\Cart
- */
- protected $cartHelper;
- /**
- * @var \Magento\Wishlist\Model\Item\OptionFactory
- */
- private $optionFactory;
- /**
- * @var \Magento\Catalog\Helper\Product
- */
- protected $productHelper;
- /**
- * @var \Magento\Framework\Escaper
- */
- protected $escaper;
- /**
- * @var \Magento\Wishlist\Helper\Data
- */
- protected $helper;
- /**
- * @var \Magento\Framework\Data\Form\FormKey\Validator
- */
- protected $formKeyValidator;
- /**
- * @param Action\Context $context
- * @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
- * @param \Magento\Wishlist\Model\LocaleQuantityProcessor $quantityProcessor
- * @param \Magento\Wishlist\Model\ItemFactory $itemFactory
- * @param \Magento\Checkout\Model\Cart $cart
- * @param \Magento\Wishlist\Model\Item\OptionFactory $optionFactory
- * @param \Magento\Catalog\Helper\Product $productHelper
- * @param \Magento\Framework\Escaper $escaper
- * @param \Magento\Wishlist\Helper\Data $helper
- * @param \Magento\Checkout\Helper\Cart $cartHelper
- * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __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
- ) {
- $this->wishlistProvider = $wishlistProvider;
- $this->quantityProcessor = $quantityProcessor;
- $this->itemFactory = $itemFactory;
- $this->cart = $cart;
- $this->optionFactory = $optionFactory;
- $this->productHelper = $productHelper;
- $this->escaper = $escaper;
- $this->helper = $helper;
- $this->cartHelper = $cartHelper;
- $this->formKeyValidator = $formKeyValidator;
- parent::__construct($context);
- }
- /**
- * 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)
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- public function execute()
- {
- /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
- $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
- if (!$this->formKeyValidator->validate($this->getRequest())) {
- return $resultRedirect->setPath('*/*/');
- }
- $itemId = (int)$this->getRequest()->getParam('item');
- /* @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');
- $postQty = $this->getRequest()->getPostValue('qty');
- if ($postQty !== null && $qty !== $postQty) {
- $qty = $postQty;
- }
- 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\ResourceModel\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;
- }
- }
|