wishlistProvider = $wishlistProvider; $this->formKeyValidator = $formKeyValidator; $this->attributeValueProvider = $attributeValueProvider ?: \Magento\Framework\App\ObjectManager::getInstance()->get(AttributeValueProvider::class); parent::__construct($context); } /** * Remove item * * @return \Magento\Framework\Controller\Result\Redirect * @throws NotFoundException */ 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('*/*/'); } $id = (int)$this->getRequest()->getParam('item'); /** @var Item $item */ $item = $this->_objectManager->create(Item::class)->load($id); if (!$item->getId()) { throw new NotFoundException(__('Page not found.')); } $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId()); if (!$wishlist) { throw new NotFoundException(__('Page not found.')); } try { $item->delete(); $wishlist->save(); $productName = $this->attributeValueProvider ->getRawAttributeValue($item->getProductId(), 'name'); $this->messageManager->addComplexSuccessMessage( 'removeWishlistItemSuccessMessage', [ 'product_name' => $productName, ] ); } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->messageManager->addError( __('We can\'t delete the item from Wish List right now because of an error: %1.', $e->getMessage()) ); } catch (\Exception $e) { $this->messageManager->addError(__('We can\'t delete the item from the Wish List right now.')); } $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate(); $refererUrl = $this->_redirect->getRefererUrl(); if ($refererUrl) { $redirectUrl = $refererUrl; } else { $redirectUrl = $this->_redirect->getRedirectUrl($this->_url->getUrl('*/*')); } $resultRedirect->setUrl($redirectUrl); return $resultRedirect; } }