_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; } }