Fromcart.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Controller\Index;
  7. use Magento\Checkout\Helper\Cart as CartHelper;
  8. use Magento\Checkout\Model\Cart as CheckoutCart;
  9. use Magento\Customer\Model\Session;
  10. use Magento\Framework\App\Action;
  11. use Magento\Framework\Data\Form\FormKey\Validator;
  12. use Magento\Framework\Escaper;
  13. use Magento\Framework\Exception\NotFoundException;
  14. use Magento\Framework\Exception\LocalizedException;
  15. use Magento\Framework\Controller\ResultFactory;
  16. use Magento\Wishlist\Controller\WishlistProviderInterface;
  17. use Magento\Wishlist\Helper\Data as WishlistHelper;
  18. /**
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class Fromcart extends \Magento\Wishlist\Controller\AbstractIndex
  22. {
  23. /**
  24. * @var WishlistProviderInterface
  25. */
  26. protected $wishlistProvider;
  27. /**
  28. * @var WishlistHelper
  29. */
  30. protected $wishlistHelper;
  31. /**
  32. * @var CheckoutCart
  33. */
  34. protected $cart;
  35. /**
  36. * @var CartHelper
  37. */
  38. protected $cartHelper;
  39. /**
  40. * @var Escaper
  41. */
  42. protected $escaper;
  43. /**
  44. * @var Validator
  45. */
  46. protected $formKeyValidator;
  47. /**
  48. * @param Action\Context $context
  49. * @param WishlistProviderInterface $wishlistProvider
  50. * @param WishlistHelper $wishlistHelper
  51. * @param CheckoutCart $cart
  52. * @param CartHelper $cartHelper
  53. * @param Escaper $escaper
  54. * @param Validator $formKeyValidator
  55. */
  56. public function __construct(
  57. Action\Context $context,
  58. WishlistProviderInterface $wishlistProvider,
  59. WishlistHelper $wishlistHelper,
  60. CheckoutCart $cart,
  61. CartHelper $cartHelper,
  62. Escaper $escaper,
  63. Validator $formKeyValidator
  64. ) {
  65. $this->wishlistProvider = $wishlistProvider;
  66. $this->wishlistHelper = $wishlistHelper;
  67. $this->cart = $cart;
  68. $this->cartHelper = $cartHelper;
  69. $this->escaper = $escaper;
  70. $this->formKeyValidator = $formKeyValidator;
  71. parent::__construct($context);
  72. }
  73. /**
  74. * Add cart item to wishlist and remove from cart
  75. *
  76. * @return \Magento\Framework\Controller\Result\Redirect
  77. * @throws NotFoundException
  78. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  79. */
  80. public function execute()
  81. {
  82. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  83. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  84. if (!$this->formKeyValidator->validate($this->getRequest())) {
  85. return $resultRedirect->setPath('*/*/');
  86. }
  87. $wishlist = $this->wishlistProvider->getWishlist();
  88. if (!$wishlist) {
  89. throw new NotFoundException(__('Page not found.'));
  90. }
  91. try {
  92. $itemId = (int)$this->getRequest()->getParam('item');
  93. $item = $this->cart->getQuote()->getItemById($itemId);
  94. if (!$item) {
  95. throw new LocalizedException(
  96. __("The cart item doesn't exist.")
  97. );
  98. }
  99. $productId = $item->getProductId();
  100. $buyRequest = $item->getBuyRequest();
  101. $wishlist->addNewItem($productId, $buyRequest);
  102. $this->cart->getQuote()->removeItem($itemId);
  103. $this->cart->save();
  104. $this->wishlistHelper->calculate();
  105. $wishlist->save();
  106. $this->messageManager->addSuccessMessage(__(
  107. "%1 has been moved to your wish list.",
  108. $this->escaper->escapeHtml($item->getProduct()->getName())
  109. ));
  110. } catch (LocalizedException $e) {
  111. $this->messageManager->addErrorMessage($e->getMessage());
  112. } catch (\Exception $e) {
  113. $this->messageManager->addExceptionMessage($e, __('We can\'t move the item to the wish list.'));
  114. }
  115. return $resultRedirect->setUrl($this->cartHelper->getCartUrl());
  116. }
  117. }