2

I have scoured the web yet I am no nearer to understanding how I add a product to the wishlist in code.

I have a product ID and a customer object in my controller and I just want to be able to add that product to that customer's wishlist. It is not rocket science I am after, just something that should be doable.

3 Answers 3

7

All the code you need can be found in app/code/core/Mage/Wishlist/controllers/IndexController.php method _addItemToWishList.

You will need to load the product by it's ID and load the wish list by customer. The code would look roughly something like this.

$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customerId, true);
$product = Mage::getModel('catalog/product')->load($productId);

$buyRequest = new Varien_Object(array()); // any possible options that are configurable and you want to save with the product

$result = $wishlist->addNewItem($product, $buyRequest);
$wishlist->save();

Just out of curiosity. Why not use the addAction provided by the wishlist controller to add the products?

You can obtain the url via echo Mage::helper('wishlist')->getAddUrl($product);

2
  • I want to do an ajax 'add to' from a modal, with things like 'how many others have it in their wishlist' being part of the display. I actually copied the addAction to 'ajaxAction' in the normal controller, so I have something that works. Just got to tidy it up and put it into my own module. Nonetheless I am surprised that there is no convenient method and that I have to extend the wishlist controller class - a bit hackish to me!!! No idea what it does with 'buyRequest', luckily I don't need it. Anyway, thanks for pointing me in the right direction!
    – Theodores
    Commented May 12, 2014 at 14:14
  • Note that if you want to add a (configured) configurable product to your wishlist the array in the Varien_Object should have the following keys: e.g. product='99', qty='1', super_attribute[133]='3'
    – Willster
    Commented Jul 15, 2015 at 11:40
0
Hi use this code to remove a product having productid **$productId** of a customer having customerid **$customerId**.

   $itemCollection = Mage::getModel('wishlist/item')->getCollection()
                        ->addCustomerIdFilter($customerId);
        foreach($itemCollection as $item) {
             if($item->getProduct()->getId() == $productId){
                $item->delete();
             }
        }
0
if($_GET['addmultiple'] == "cartitem")
{


        $cuu = $this->helper('core/url')->getCurrentUrl();
        $currentUrl = str_replace("?addmultiple=cartitem","",$cuu);

    try{
            $cart = Mage::helper('checkout/cart')->getCart();
            $ms="";
            $myupsell_product = $_product->getUpSellProductCollection();
            $myupsell_product->AddStoreFilter();
            foreach($myupsell_product as $upsellprd)
        {

            $params = 1;
            $pid = $upsellprd->getId();
            $product = Mage::getModel('catalog/product')->load($pid);;
            $cart->addProduct($product, $params);
            $msg .= $product->getName(). " is successfully added into cart<br>";
        }
         $cart->save();
         Mage::getSingleton('core/session')->addSuccess(Mage::helper('checkout')->__($msg));
         header("Refresh: 0; url=$currentUrl");     


    }
    catch(Exception $e) 
    {
        Mage::getSingleton('core/session')->addError(Mage::helper('checkout')->__($e->getMessage()));
        header("Refresh: 0; url=$currentUrl");     


    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.