问题
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.
回答1:
I have no time to debug whatever goes wrong with your code, but using the normal convention of deleting entities should work:
Mage::getModel('wishlist/item')->load($id)->delete();
回答2:
Just have a look at the removeAction
of Mage_Wishlist_IndexController
.
You have to load the Wishlist item by its ID and then you can call the delete()
method.
回答3:
I know this question is old but since I just ran into the same problem with magento 1.7.0.2 I want to share the solution.
To make it work you need to use the method updateItem as follow
$wishlist->updateItem($id, array('qty' => 10));
Instead of
$wishlist->updateItem($id, null, array('qty' => 10));
You can't use this method to set an item qty to 0. It will automatically set it to a minimum of 1 unless you use the delete method.
回答4:
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();
}
}
来源:https://stackoverflow.com/questions/7145208/magento-wishlist-remove-item