I'm trying to make a widget using the wishlist to show whether you've added the current product to the wishlist already or not. I've seen some other threads with answers and I can safely say I tried most if not all of them. Please help a beginning Magento 2 Dev out 🙏🏼
Wishlist has 3 items (Can be seen in the wishlist page and Database)
/**
* @param Product $inputProduct
*
* @return WishlistCollectionFactory
*/
public function checkWishlistItem(Product $inputProduct)
{
$wishlistCollection = $this->_wishlistProvider->getWishlist()->getItemCollection();
$wishlistItemList = [];
/** @var Product $wishlistItem */
foreach ($wishlistCollection as $wishlistItem) {
array_add($wishlistItemList, 'product_id', $wishlistItem->getId());
}
return $wishlistCollection->addFieldToFilter('main_table.product_id', $inputProduct->getId());
}
Other block I've tried:
$wishlistCollection = $this->_wishlistHelper->getWishlistItemCollection();
$inWishlist = false;
foreach ($wishlistCollection as $wishlistItem) {
if ($inputProduct->getId() == $wishlistItem->getProduct()->getId()) {
$inWishlist = true;
}
}
I have created a single php file that does all the work in Block/Widget. I can use all the feedback I can get so feel free to tell me another way of doing things if you feel like I'm doing things wrong.