FBHY
FBHY

Reputation: 1004

Magento - Wishlist addNewItem remove all my items

I got a problem with my whislist. Here is the thing.
I try to programmatically add into a whistlist, a new Item.
So I use the $whishlist->addNewItem() function. The product is added, but if I had other products in my whislist, they've been removed!

Here is what I use:

<?php
  $customer = Mage::getModel('customer/customer')->load($my_customer_id);
  $product = Mage::getModel('catalog/product')->load($my_product_id);
  $wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);  

  $request = new Varien_Object(array());
  $result = $wishlist->addNewItem($product, $request);  

  $wishlist->save();
?>

Did I do something wrong?

Thank you guys

EDIT:
I Finally found, it seems weird, but in my case, I have to remove this line $wishlist->save();

Upvotes: 2

Views: 636

Answers (5)

Anthony BONNIER
Anthony BONNIER

Reputation: 355

I had the same problem and the "save" function was not in cause.

My problem came from the products redindexation. I've found my solution here : Why is my wish list limited to one item?

Reindexing every indexes made my wishlist worked again for products recently added through the rest API.

To do it automatically, I've change this index to "Update on Save" in the admin : Product Flat Data - Reorganize EAV product structure to flat structure.

My wishlist works perfectly fine now (even with the "save" function).

Upvotes: 0

Ajay
Ajay

Reputation: 458

I too had the same problem. But in my case product visibility in magento backend is set to "Not Visible Individually". changed product visibility and it worked.

Upvotes: 0

FBHY
FBHY

Reputation: 1004

I had to remove this line $wishlist->save();
I don't know if it's THE solution, but it works for me.
Thank's to everyone :)

Upvotes: 1

user3013440
user3013440

Reputation: 108

Remove the ->save on the wishlist. Sometimes it works

Upvotes: 0

Mayuresh
Mayuresh

Reputation: 81

This might help you, Lets say you have to add a product to wishlist on click so it would be like :

 <a onclick="setLocation('<?php echo $this->helper('wishlist')->getAddUrl(Mage::registry('current_product'));?>')">add to wishlist</a>

Upvotes: 0

Related Questions