I want to remove products from wishlist if it is already added to wishlist in Product detail page after clicking wishlist icon in magento2.3.0?
Any help would be appreciated.
I want to remove products from wishlist if it is already added to wishlist in Product detail page after clicking wishlist icon in magento2.3.0?
Any help would be appreciated.
Check here to verify if item is already in wishlist. If it is present in wishlist, you can use below code to remove the item from wishlist:
<?php
namespace Vendor\General\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
class RemoveFromWishlist extends Action {
protected $wishlist;
public function __construct(
Context $context,
\Magento\Customer\Model\Session $customerSession,
\Magento\Wishlist\Model\Wishlist $wishlist,
\Magento\Framework\Controller\Result\JsonFactory $jsonFactory
) {
$this->customerSession = $customerSession;
$this->wishlist = $wishlist;
$this->jsonFactory = $jsonFactory;
parent::__construct($context);
}
public function execute()
{
$customerId = $this->customerSession->getCustomer()->getId();
if(!$customerId) {
$jsonData = ['result' => ['status' => 200, 'message' => 'Customer not logged in.']];
$result = $this->jsonFactory->create()->setData($jsonData);
return $result;
}
$productId = $this->getRequest()->getParam('productId');
$wish = $this->wishlist->loadByCustomerId($customerId);
$items = $wish->getItemCollection();
/** @var \Magento\Wishlist\Model\Item $item */
foreach ($items as $item) {
if ($item->getProductId() == $productId) {
$item->delete();
$wish->save();
}
}
$jsonData = ['result' => ['status' => 200, 'message' => 'Removed from wishlist']];
$result = $this->jsonFactory->create()->setData($jsonData);
return $result;
}
}
You can create events.xml
file in your custom module here
app/code/Vendor/Module/etc/events.xml
Content for this file is..
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch_wishlist_index_add">
<observer name="wishlist_index_add_before" instance="Vendor\Module\Observer\RemoveWishlistItemBeforeAdd" />
</event>
</config>
Now you need to create one Observer file here
app/code/Vendor/Module/Observer/RemoveWishlistItemBeforeAdd.php
Content for this file is..
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;
class RemoveWishlistItemBeforeAdd implements ObserverInterface
{
protected $customerSession;
protected $wishlist;
public function __construct(
\Magento\Customer\Model\Session $customerSession,
\Magento\Wishlist\Model\Wishlist $wishlist
) {
$this->customerSession = $customerSession;
$this->wishlist = $wishlist;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$customerId = $this->customerSession->getCustomer()->getId();
$currentProductId = $observer->getRequest()->getParam('product');
if($customerId){
$currentCustomerWishlist = $this->wishlist->loadByCustomerId($customerId);
$wishlistItems = $currentCustomerWishlist->getItemCollection();
foreach ($wishlistItems as $wishlistItem) {
if ($wishlistItem->getProductId() == $currentProductId) {
$wishlistItem->delete();
}
}
}
return $observer;
}
}
You can download this module as well.. Download
Hope this will help!