cancel
Showing results for 
Search instead for 
Did you mean: 

Magento2 check if product is in wishlist

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Magento2 check if product is in wishlist

Hi, I need to check if on catalog category page and product view page if the product is in wishlist. If in wishlist I need to add a different icon. Can somebody help me figure this out? thanks

7 REPLIES 7

Re: Magento2 check if product is in wishlist

When doing this, you'll want to avoid breaking the full page caching of the page. You'll want to find out what products are in the customer's wishlist via ajax and then store the results in a cookie/local storage for future use. E.g. https://magento.stackexchange.com/questions/144851/check-if-product-is-in-wishlist/144856#144856

----
If you've found one of my answers useful, please give "Kudos" or "Accept as Solution" as appropriate. Thanks!

Re: Magento2 check if product is in wishlist

can it be done any other way?


@Tom Robertshaw wrote:

When doing this, you'll want to avoid breaking the full page caching of the page. You'll want to find out what products are in the customer's wishlist via ajax and then store the results in a cookie/local storage for future use. E.g. https://magento.stackexchange.com/questions/144851/check-if-product-is-in-wishlist/144856#144856


 

Re: Magento2 check if product is in wishlist

<?php if($_category_config['addtowishlist']): ?>
<?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow()): ?>
<a href="#"
class="action towishlist actions-secondary"
title="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>"
aria-label="<?php echo $block->escapeHtml(__('Add to Wish List')); ?>"
data-post='<?php echo $block->getAddToWishlistParams($_product); ?>'
data-action="add-to-wishlist"
role="button">
<span><?php echo __('Add to Wish List') ?></span>
</a>
<?php endif; ?>
<?php endif; ?>

 

Would like to change class inside ATAG if its not in wishlist, so any idea how to do it here on catalog product list page and also on catalog product view page.

Re: Magento2 check if product is in wishlist

There's no other way to do it without breaking full page cache or making that block always uncached which would degrade performance. As soon as you need the site to change based off a customers session, you need this kind of solution.

----
If you've found one of my answers useful, please give "Kudos" or "Accept as Solution" as appropriate. Thanks!

Re: Magento2 check if product is in wishlist

i would also like to add to wishlist and remove from wishlist from same place. what do i do?

Re: Magento2 check if product is in wishlist

To check if a product is on the wishlist on the catalog category page and product view page, you need to:

Verify User Login – Wishlist functionality works for logged-in users.
Fetch Wishlist Items – Retrieve the user’s wishlist and compare product IDs.
Display the Right Icon – If the product is on the wishlist, show a different icon.

You can achieve this by using Magento’s wishlist model in your theme’s template files. 

Re: Magento2 check if product is in wishlist

<php
/** @var \Magento\Wishlist\Helper\Data $withListHelper **/
$withListHelper = $this->helper(\Magento\Wishlist\Helper\Data::class);
  
$wishListCollection = $withListHelper->getWishlistItemCollection()->
            addFilterToMap('product_id', 'main_table.product_id')->
            addFieldToFilter('product_id', $_product->getId());

$inWishList = !empty($wishListCollection->getSize());
$wishListCollection->clear();