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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Wishlist\Controller\Index;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Action;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Controller\ResultFactory;
use Magento\Wishlist\Controller\WishlistProviderInterface;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class UpdateItemOptions extends \Magento\Wishlist\Controller\AbstractIndex
{
/**
* @var WishlistProviderInterface
*/
protected $wishlistProvider;
/**
* @var Session
*/
protected $_customerSession;
/**
* @var ProductRepositoryInterface
*/
protected $productRepository;
/**
* @var Validator
*/
protected $formKeyValidator;
/**
* @param Action\Context $context
* @param Session $customerSession
* @param WishlistProviderInterface $wishlistProvider
* @param ProductRepositoryInterface $productRepository
* @param Validator $formKeyValidator
*/
public function __construct(
Action\Context $context,
Session $customerSession,
WishlistProviderInterface $wishlistProvider,
ProductRepositoryInterface $productRepository,
Validator $formKeyValidator
) {
$this->_customerSession = $customerSession;
$this->wishlistProvider = $wishlistProvider;
$this->productRepository = $productRepository;
$this->formKeyValidator = $formKeyValidator;
parent::__construct($context);
}
/**
* Action to accept new configuration for a wishlist item
*
* @return \Magento\Framework\Controller\Result\Redirect
*/
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('*/*/');
}
$productId = (int)$this->getRequest()->getParam('product');
if (!$productId) {
$resultRedirect->setPath('*/');
return $resultRedirect;
}
try {
$product = $this->productRepository->getById($productId);
} catch (NoSuchEntityException $e) {
$product = null;
}
if (!$product || !$product->isVisibleInCatalog()) {
$this->messageManager->addError(__('We can\'t specify a product.'));
$resultRedirect->setPath('*/');
return $resultRedirect;
}
try {
$id = (int)$this->getRequest()->getParam('id');
/* @var \Magento\Wishlist\Model\Item */
$item = $this->_objectManager->create(\Magento\Wishlist\Model\Item::class);
$item->load($id);
$wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
if (!$wishlist) {
$resultRedirect->setPath('*/');
return $resultRedirect;
}
$buyRequest = new \Magento\Framework\DataObject($this->getRequest()->getParams());
$wishlist->updateItem($id, $buyRequest)->save();
$this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
$this->_eventManager->dispatch(
'wishlist_update_item',
['wishlist' => $wishlist, 'product' => $product, 'item' => $wishlist->getItem($id)]
);
$this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
$message = __('%1 has been updated in your Wish List.', $product->getName());
$this->messageManager->addSuccess($message);
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addError(__('We can\'t update your Wish List right now.'));
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
}
$resultRedirect->setPath('*/*', ['wishlist_id' => $wishlist->getId()]);
return $resultRedirect;
}
}