1

I am trying to add products from the admin create order screen straight into the users wishlist programmatically however it doesn't seem to be working.

$quote = Mage::getSingleton('adminhtml/session_quote');
$currentQuote = $quote->getQuote();
$customer = $currentQuote->getCustomer();

$items = $currentQuote->getAllVisibleItems();

$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer->getId(), true);
Mage::register('wishlist', $wishlist);

foreach ($items as $item)
{
    $productId = $item->getProductId();
    $product = Mage::getModel('catalog/product')->load($productId);
        $buyRequest = $item->getBuyRequest();
        $result = $wishlist->addNewItem($product, $buyRequest);

    if(is_string($result))
    {
        Mage::throwException($result);
    }
    $wishlist->save();

    Mage::dispatchEvent(
            'wishlist_add_product',
            array(
                'wishlist' => $wishlist,
                'product' => $product,
                'item' => $result
            )
        );

        Mage::helper('wishlist')->calculate();
}       

Mage::getSingleton('core/session')->addSuccess("Quote sent successfully to customer!");

Mage::app()->getResponse()->setRedirect(Mage::helper('adminhtml')->getUrl('adminhtml/sales_order/index'));

I'm not to sure what is going on. I am getting no errors in the log or exceptions being thrown however no products end up in the customer's wishlist.

1 Answer 1

1

To add a product to customer's wishlist you simply need to initiate the wishlist model and call the addNewItem method passing the product object. This may help you

  $customer = Mage::getModel('customer/customer');
  $wishlist = Mage::getModel('wishlist/wishlist');
  $product = Mage::getModel('catalog/product');

  $customer_id = 1;
  $product_id = 1;
  $customer->load($customer_id);
  $wishlist->loadByCustomer($customer_id);
  $wishlist->addNewItem($product->load($product_id));

You can use this codes wherever you want.

2
  • I guess this is already exactly what he is doing and more...
    – 7ochem
    Commented Feb 20, 2015 at 13:03
  • Yes I agree with you on that @7ochem, but he is creating a buy request for every item in the wishlist but I think the problem was with items not being added to wishlist
    – Tarun
    Commented Feb 20, 2015 at 19:03

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.