Magento愿望列表-删除项目

Magento愿望列表-删除项目,magento,magento-1.5,Magento,Magento 1.5,我已经构建了一个自定义脚本,使用AJAX向愿望列表添加和删除项目。添加产品不是问题,但我不知道如何删除项目。Magento版本是1.5.1.0 脚本位于/scripts/中,如下所示: include_once '../app/Mage.php'; Mage::app(); try{ $type = (!isset($_GET['type']))? 'add': $_GET['type']; $id = (!isset($_GET['id']))? '': $_GET['id

我已经构建了一个自定义脚本,使用AJAX向愿望列表添加和删除项目。添加产品不是问题,但我不知道如何删除项目。Magento版本是
1.5.1.0

脚本位于
/scripts/
中,如下所示:

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);
}
因此,当我请求将“remove”作为
$type
并将愿望列表项id作为$id的脚本时,我得到以下错误:

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

当我查看
/app/code/core/Mage/Wishlist/Model/Wishlist.php
中的函数
updateItem()
时,它需要一个“buyRequest”,但我不知道那是什么。

我没有时间调试代码中出现的任何错误,但是使用删除实体的常规惯例应该可以:

Mage::getModel('wishlist/item')->load($id)->delete();

只需查看
Mage\u Wishlist\u IndexController
removeAction

您必须按其ID加载愿望列表项,然后可以调用
delete()
方法。

我知道这个问题很老,但由于我刚刚遇到了与magento 1.7.0.2相同的问题,我想分享解决方案

要使其工作,您需要使用updateItem方法,如下所示

$wishlist->updateItem($id, array('qty' => 10));
而不是

$wishlist->updateItem($id, null, array('qty' => 10));

无法使用此方法将物料数量设置为0。除非您使用delete方法,否则它将自动将其设置为最小值1。

Hi使用此代码删除具有customerid的客户的productid
$productid
产品

$itemCollection = Mage::getModel('wishlist/item')->getCollection()
    ->addCustomerIdFilter($customerId);
foreach($itemCollection as $item) {
    if($item->getProduct()->getId() == $productId){
        $item->delete();
    }
}

为了在Magento中使用Ajax从愿望列表中删除产品,我创建了一个自定义函数。有关更多详细信息,请参阅: