Reputation: 191
Magento ver. 1.9.1.0 .
hide wishlist button if item is already in current customers wishlist or change it to filled heart
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<div class="ajax-wishlist">
<a href="#"
onclick='ajaxWishlist("<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>","<?php echo $_product->getId()?>");return false;' class="link-wishlist" title="Add to Wishlist">
<i class="fa fa-heart-o"></i>
</a>
</div>
<?php endif; ?>
further i would like to update wishlist button dynamically. ie fa-heart-o TO fa-heart as soon as wishlist is updated.. [new to magento]
reference Check whether a product is in the wishlist or not
I tried this.
<?php $wishlist = Mage::getModel('wishlist/item')->load($_product->getId(),'product_id');
if($wishlist->getId())
//product is added
echo "Added! - Product is in the wishlist!";
else
//add product to wishlist
echo "<a href='".$this->helper('wishlist')->getAddUrl($_product) ."'>Add This?</a>";
;?>
It shows all products that have been added to the wishlist by any customers. and how to use above code for homepage products? ie new arrival, most viewed etc. It shows error.call to a member function getId() on non object
Upvotes: 1
Views: 4125
Reputation: 765
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.
Upvotes: 0