Wednesday, April 18, 2012

Magento Wishlist - Remove item

I've built a custom script to add and remove items to the wishlist using AJAX. Adding products is not a problem but I can't figure out how to remove an item. The Magento version is 1.5.1.0.



The script is in /scripts/ and looks like this:



include_once '../app/Mage.php';

Mage::app();

try{
$type = (!isset($_GET['type']))? 'add': $_GET['type'];
$id = (!isset($_GET['id']))? '': $_GET['id'];

$session = Mage::getSingleton('core/session', array('name'=>'frontend'));
$_customer = Mage::getSingleton('customer/session')->getCustomer();

if ($type != 'remove') $product = Mage::getModel('catalog/product')->load($id);

$wishlist = Mage::helper('wishlist')->getWishlist();

if ($id == '')
exit;

if ($type == 'add')
$wishlist->addNewItem($product);
elseif ($type == 'remove')
$wishlist->updateItem($id,null,array('qty' => 0));

$products = Mage::helper('wishlist')->getItemCount();
if ($type == 'add') $products++;
if ($type == 'remove') $products--;

$result = array(
'result' => 'success',
'type' => $type,
'products' => $products,
'id' => $id
);

echo json_encode($result);
}
catch (Exception $e)
{
$result = array(
'result' => 'error',
'message' => $e->getMessage()
);
echo json_encode($result);
}


So when I request the script with "remove" as $type and the wishlist item id as $id I get the following error:



Fatal error: Call to a member function getData() on a non-object in /[magento path]/app/code/core/Mage/Catalog/Helper/Product.php on line 389


When I look at the function updateItem() in /app/code/core/Mage/Wishlist/Model/Wishlist.php it expects a "buyRequest", but I can't figure out what that is.





No comments:

Post a Comment