0

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.

3
  • You want to remove wishlist item first if that already exist instead of changing item's QTY right ? Commented Nov 18, 2019 at 7:43
  • Yes your are right.
    – Amy
    Commented Nov 18, 2019 at 7:44
  • Okay let me check and ping you. Commented Nov 18, 2019 at 7:45

2 Answers 2

0

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;
    }
}
2
  • May I know where to apply this code.
    – Amy
    Commented Nov 18, 2019 at 8:58
  • In the pdp template where wishlist code is present, check if item is already in wishlist. If present, send a ajax request to custom controller which will remove item from wishlist. Custom controller code is shared above.
    – amitshree
    Commented Nov 18, 2019 at 9:11
0

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!

9
  • let me know if it helps. Commented Nov 18, 2019 at 9:05
  • I have implemented this code not working..
    – Amy
    Commented Nov 18, 2019 at 9:31
  • Can you please download my module and install that and check once ? Commented Nov 18, 2019 at 9:33
  • I have applied your downloaded module only..
    – Amy
    Commented Nov 18, 2019 at 9:34
  • Can you please use echo die in observer and then check my observer file is calling when you click on add to wishlist button ? Commented Nov 18, 2019 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.