Check if Product is Already on Wishlist in Magento 2 – How to Guide

magento2wishlist

In Magento 1.x I did it like this:

$_product = ...; // some product object you already have
$_productCollection = Mage::helper('wishlist')->getProductCollection()
    ->addFieldToFilter('sku', $_product->getSku());
if($_productCollection->count() > 0) {
    // User already has item in wishlist.
}

How can I achieve the same in Magento 2?

Best Answer

As far as I know, we can use \Magento\Wishlist\Helper\Data to check if a product is already on the wishlist.

/** @var \Magento\Wishlist\Helper\Data $wishlistHelper **/

$wishlistHelper->getWishlistItemCollection()
              ->addFieldToFilter('product_id', $_product->getId());

Remember to inject this class in your construction.

Related Topic