How To

How to Prevent Adding Duplicate Product to Wishlist in Magento 2

Hello Magento Friends,

Hope everything is groovy and fab at your end. Today I will help you to solve How to Prevent Adding Duplicate Product to Wishlist in Magento 2.

In default Magento 2, if the customer adds any product to the wishlist it also allows repetitive products to be added to the wishlist. The Magento 2 store admin can still delete the duplicate products in the wishlist and also prevent adding the same. The customers can easily manage their wishlist by preventing duplicate items to be added.

So, let’s get started with the steps on How to Prevent Adding Duplicate Product to Wishlist in Magento 2.

Steps to Prevent Adding Duplicate Product to Wishlist in Magento 2:

Step 1: Firstly, go to the below path

app\code\Vendor\Extension\etc\events.xml

Now, add the below code

<?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\Extension\Observer\WishlistItemBeforeAdd" />
    </event>
</config>

Step 2: Next, move to the below path

app\code\Vendor\Extension\Observer\WishlistItemBeforeAdd.php

And add the code as mentioned below

<?php

namespace Vendor\Extension\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;

class WishlistItemBeforeAdd 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)
    {


        $customer_id = $this->customerSession->getCustomer()->getId();
        $currentProduct_id = $observer->getRequest()->getParam('product');

        if($customer_id)
        {
            $currentCustomerWishlist = $this->wishlist->loadByCustomerId($customer_id);
            $wishlistItems = $currentCustomerWishlist->getItemCollection();

            foreach ($wishlistItems as $wishlistItem)
            {
                if ($wishlistItem->getProductId() == $currentProduct_id)
                {
                    $wishlistItem->delete();
                }
            }
        }
        return $observer;
   }  
}

Conclusion:

Hopefully, all are able to Prevent Adding Duplicate Product to Wishlist in Magento 2. For any difficulty, while executing the code, do let me know in the comment sections below. Ensure you share the article with your Magento colleagues and on your social media platforms.

Stay in touch for future updates! 

Happy Coding!

Click to rate this post!
[Total: 8 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

2 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

2 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

3 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

5 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

5 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

6 days ago