Magento: How to check if product is in the wishlist?

March 13th, 2014 by in Frontend, Magento 4
wishlist

If you need to check if a product is in the wishlist and display that information instead of the default Magento “Add to Wishlist” link, this article is for you. To simplify things I will explain how to detect if a product is in the wishlist on category and product page but the principle is the same for any other pager or block. You just need to be sure that you have the product id available. Everything else should work fine on any page.

Category page

  • Open template/catalog/product/list.phtml
  • Find the wishlist link code. If you are using the base theme it should look something like this:
    <?php if ($this->helper('wishlist')->isAllow()) : ?>
     <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
    <?php endif; ?>
    
  • Replace it with:
    <?php if ($this->helper('wishlist')->isAllow()) : ?>
     <?php foreach (Mage::helper('wishlist')->getWishlistItemCollection() as $_wishlist_item): ?>
      <?php $_in_wishlist = false; ?>
      <?php if($_product->getId() == $_wishlist_item->getProduct()->getId()): ?>
       <?php $_in_wishlist = true; break; ?>
      <?php endif; ?>
     <?php endforeach; ?>
    
     <?php if(!empty($_in_wishlist) && $_in_wishlist): ?>
      <li><span class="in-wishlist"><?php echo $this->__('Already in Wishlist') ?></span></li>
     <?php else: ?>
      <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
     <?php endif; ?>
    <?php endif; ?>
    

Product page

  • Open template/catalog/product/view/addto.phtml
  • Find the wishlist link code. If you are using the base theme it should look something like this:
    <?php if ($this->helper('wishlist')->isAllow()) : ?>
        <li><a href="<?php echo $_wishlistSubmitUrl ?>" onclick="productAddToCartForm.submitLight(this, this.href); return false;" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
    <?php endif; ?>
    
  • Replace it with:
    <?php if ($this->helper('wishlist')->isAllow()) : ?>
     <?php foreach (Mage::helper('wishlist')->getWishlistItemCollection() as $_wishlist_item): ?>
      <?php $_in_wishlist = false; ?>
      <?php if($_product->getId() == $_wishlist_item->getProduct()->getId()): ?>
       <?php $_in_wishlist = true; break; ?>
      <?php endif; ?>
     <?php endforeach; ?>
    
     <?php if(!empty($_in_wishlist) && $_in_wishlist): ?>
      <li><span class="is-wishlist"><?php echo $this->__('Already in Wishlist') ?></span></li>
     <?php else: ?>
      <li><a href="<?php echo $_wishlistSubmitUrl ?>" onclick="productAddToCartForm.submitLight(this, this.href); return false;" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
     <?php endif; ?>
    <?php endif; ?>
    

 

That’s it. As I said above you can use this method to check if the product is in the wishlist on any Magento page or block as long as you have a product id available.

Written by Srdjan Stojiljkovic

Srdjan is a Frontend Developer with a passion for pixel perfect design and great user interfaces. He also designed and developed this site and is dedicated to provide best and most useful articles about development in general.


See all articles from this author »

Comment (4)

Posted on March 25, 2014 by Andre

Awesome, thank you for sharing! Works perfectly

Posted on November 24, 2015 by purvi

Awesome, thank you for sharing! Works perfectly

Posted on November 24, 2015 by purvi

Is it possible to check on cart page?

Posted on November 24, 2015 by Srdjan Stojiljkovic

It should work on the cart page. If you get the product id properly everything else is just using a wishlist helper.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>