Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateItemOptions.php
Go to the documentation of this file.
1 <?php
7 
15 
20 {
24  protected $wishlistProvider;
25 
29  protected $_customerSession;
30 
34  protected $productRepository;
35 
39  protected $formKeyValidator;
40 
48  public function __construct(
49  Action\Context $context,
50  Session $customerSession,
54  ) {
55  $this->_customerSession = $customerSession;
56  $this->wishlistProvider = $wishlistProvider;
57  $this->productRepository = $productRepository;
58  $this->formKeyValidator = $formKeyValidator;
59  parent::__construct($context);
60  }
61 
67  public function execute()
68  {
70  $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
71  if (!$this->formKeyValidator->validate($this->getRequest())) {
72  return $resultRedirect->setPath('*/*/');
73  }
74 
75  $productId = (int)$this->getRequest()->getParam('product');
76  if (!$productId) {
77  $resultRedirect->setPath('*/');
78  return $resultRedirect;
79  }
80 
81  try {
82  $product = $this->productRepository->getById($productId);
83  } catch (NoSuchEntityException $e) {
84  $product = null;
85  }
86 
87  if (!$product || !$product->isVisibleInCatalog()) {
88  $this->messageManager->addError(__('We can\'t specify a product.'));
89  $resultRedirect->setPath('*/');
90  return $resultRedirect;
91  }
92 
93  try {
94  $id = (int)$this->getRequest()->getParam('id');
95  /* @var \Magento\Wishlist\Model\Item */
96  $item = $this->_objectManager->create(\Magento\Wishlist\Model\Item::class);
97  $item->load($id);
98  $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
99  if (!$wishlist) {
100  $resultRedirect->setPath('*/');
101  return $resultRedirect;
102  }
103 
104  $buyRequest = new \Magento\Framework\DataObject($this->getRequest()->getParams());
105 
106  $wishlist->updateItem($id, $buyRequest)->save();
107 
108  $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
109  $this->_eventManager->dispatch(
110  'wishlist_update_item',
111  ['wishlist' => $wishlist, 'product' => $product, 'item' => $wishlist->getItem($id)]
112  );
113 
114  $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
115 
116  $message = __('%1 has been updated in your Wish List.', $product->getName());
117  $this->messageManager->addSuccess($message);
118  } catch (\Magento\Framework\Exception\LocalizedException $e) {
119  $this->messageManager->addError($e->getMessage());
120  } catch (\Exception $e) {
121  $this->messageManager->addError(__('We can\'t update your Wish List right now.'));
122  $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
123  }
124  $resultRedirect->setPath('*/*', ['wishlist_id' => $wishlist->getId()]);
125  return $resultRedirect;
126  }
127 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$message
$wishlist
Definition: wishlist.php:10
foreach($product->getExtensionAttributes() ->getBundleProductOptions() as $option) $buyRequest
__construct(Action\Context $context, Session $customerSession, WishlistProviderInterface $wishlistProvider, ProductRepositoryInterface $productRepository, Validator $formKeyValidator)