Add/Remove Product from Wishlist using Ajax in Magento

Hello Guys,

There are many custom codes and extension(s) are available on Internet through which we can Add product in "WISHLIST" using Ajax but vice-versa is not available. i.e, we can not Remove product from WISHLIST using Ajax.So I have created a custom functionality which works fine for me. Hope this will help some one!!

I have extended VS AJAX extension to achieve "Remove Product from wishlist using AJAX".You can find VS AJAX extension @ http://www.magentocommerce.com/magento-connect/vs-ajax-8468.html

1) Download my Attachment and add it in your respective magento folder structure.

2)   After than Add below code in under your product list page

<?php
## Add this in top of your product list page ##
$loggedin_cus_id=Mage::helper('customer')->getCustomer()->getEntityId() ;
## Find logged in  sessions user id


## Finding users wishlist ##
$wishList = Mage::getSingleton('wishlist/wishlist')->loadByCustomer($loggedin_cus_id);//customer
$wishListItemCollection = $wishList->getItemCollection();
if (count($wishListItemCollection)) {
$arrProductIds = array();
foreach ($wishListItemCollection as $item) {
/* @var $product Mage_Catalog_Model_Product */
$product = $item->
getProduct();
$arrProductIds[] = $product->
getId();
}
}

/**************  Wishlist code *********************************************/
$wishlistcoll =   Mage::helper('wishlist')->
getWishlistItemCollection();
$wishlistcollarray =   Mage::helper('wishlist')->getWishlistItemCollection()->getData();
foreach($wishlistcoll as $item){
$itemarray[] = $item->getProductId();
}

## Add this in top of your product list page ##
?>

<?php // -----------------------------------------------------------------------
## Under  foreach loop Add  below code in place of existing wishlist code ?>

<?php if(Mage::getStoreConfig('ajax/wishlistcompare/enabledpro')){?>

<div style="float:left;">

  <ul class="add-to-links">
  <?php if ($this->helper('wishlist')->isAllow()) : ?>                     
                     
<?php  if (in_array($_product->getId(), $arrProductIds)) {
$checkUser=1;
}else {                              
$checkUser=0;
}

$wishlist = Mage::getModel('wishlist/item')->load($_product->getId(),'product_id');

if($wishlist->getId() && $checkUser==1) {

if (in_array($_product->getId(), $itemarray))
{
$itemId = $wishlistcollarray[$count]['wishlist_item_id'];

$itemLoad = Mage::getModel('wishlist/item')->load($itemId);
?>
<li style="float: left; display: block; width: 100%; text-align: center;">
<a href="#" onclick='removeWishlist("<?php echo $this->helper('wishlist')->getRemoveUrl($itemLoad) ?>","<?php echo $_product->getId()?>");return false;' class="link-wishlist">
  <span class="fabShopSprite selected">
  </span>
  <?php //echo $this->__('Remove to Wishlist') ?>
</a>
</li>
                     
<?php
$count++;
}
?>                    
                     
<?php } else { ?>
<li style="float: left; display: block; width: 100%; text-align: center;">
<a href="#" onclick='ajaxWishlist("
<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>","<?php echo $_product->getId()?>");return false;' class="link-wishlist">
  <span class="fabShopSprite" id="wishlist<?php echo $_product->getId()?>">
  </span>
  </a>
</li>
 <?php } ?>
<?php endif; ?>
</ul>
<span id='ajax_loading<?php echo $_product->getId()?>' style='display:none'>
  <img src='<?php echo $this->getSkinUrl('images/opc-ajax-loader.gif')?>'/>
</span>
</div>
<?php }  ?>
<?php ##******************************************************?>

3) Please Note: You need to create CSS & 2 heart icon images by yourself for displaying Heart icon (Wishlist icon - from where we can add/ remove product from wishlist)

Comments

  1. Hello Prasad
    I have created code of Add/Remove Product from Wishlist using Ajax in my project using this tutorial and it is working so fine and looking grate thank you very much.

    Thank You.
    Suresh Chikani

    ReplyDelete
  2. Thanks Prasad for such a helpful information.

    Suyash.

    ReplyDelete
  3. Very good start buddy..keep it up :)

    ReplyDelete
  4. Gr8 buddy !!
    Best of luck and keep it up.

    ReplyDelete
  5. Good to see this. Feeling Great.

    ReplyDelete
  6. Hello Prasad.. Thanks for provide such helpful code and guidence its really helped me out and it saved my time too..
    Keep it up.. :)

    ReplyDelete
  7. I've added all your files, but i get error ReferenceError: ajaxWishlist is not defined.
    Js file is not connected, can you help me?

    ReplyDelete
    Replies
    1. Hi Evgeny

      Thanks for your feedback, I will check this issue and get back to you on this soon.

      Delete
  8. Hi Prasad. Are you available this weekend? I attempted to add it but it doesn't work at all. :(

    ReplyDelete
  9. I want to delete product from cart sidebar using ajax....Can you help...?

    ReplyDelete
  10. when wishlist link is clicked i am getting error Uncaught ReferenceError: removeWishlist is not defined
    at HTMLAnchorElement.onclick (VM10579 rings:616)
    onclick @ VM10579 rings:616.... and while adding product into wishlist getting invalid token error

    ReplyDelete

Post a Comment

Popular posts from this blog

Terraform Basics - To get Started

Send Promo Code to New Registered Customer in Magento