/
Fromcart.php
85 lines (75 loc) · 3.16 KB
/
Fromcart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Wishlist\Controller\Index;
use Magento\Framework\App\Action;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Wishlist\Controller\IndexInterface;
use Magento\Framework\Controller\ResultFactory;
class Fromcart extends Action\Action implements IndexInterface
{
/**
* @var \Magento\Wishlist\Controller\WishlistProviderInterface
*/
protected $wishlistProvider;
/**
* @param Action\Context $context
* @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
*/
public function __construct(
Action\Context $context,
\Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
) {
$this->wishlistProvider = $wishlistProvider;
parent::__construct($context);
}
/**
* 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.'));
}
$itemId = (int)$this->getRequest()->getParam('item');
/* @var \Magento\Checkout\Model\Cart $cart */
$cart = $this->_objectManager->get('Magento\Checkout\Model\Cart');
$session = $this->_objectManager->get('Magento\Checkout\Model\Session');
try {
$item = $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);
$productIds[] = $productId;
$cart->getQuote()->removeItem($itemId);
$cart->save();
$this->_objectManager->get('Magento\Wishlist\Helper\Data')->calculate();
$productName = $this->_objectManager->get('Magento\Framework\Escaper')
->escapeHtml($item->getProduct()->getName());
$wishlistName = $this->_objectManager->get('Magento\Framework\Escaper')
->escapeHtml($wishlist->getName());
$this->messageManager->addSuccess(__("%1 has been moved to wish list %2", $productName, $wishlistName));
$wishlist->save();
} catch (LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addException($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->_objectManager->get('Magento\Checkout\Helper\Cart')->getCartUrl());
}
}