2

我已经构建了一个自定义脚本来使用 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

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

4

4 回答 4

3

我没有时间调试您的代码出现的任何问题,但是使用删除实体的常规约定应该可以工作:

Mage::getModel('wishlist/item')->load($id)->delete();
于 2011-08-22T09:59:06.087 回答
1

看看. removeAction_ Mage_Wishlist_IndexController您必须通过其 ID 加载 Wishlist 项目,然后才能调用该delete()方法。

于 2011-08-22T10:06:44.907 回答
0

我知道这个问题很老,但是因为我刚刚遇到了与 magento 1.7.0.2 相同的问题,所以我想分享解决方案。

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

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

代替

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

您不能使用此方法将项目 qty 设置为 0。除非您使用 delete 方法,否则它将自动将其设置为最小 1。

于 2014-01-09T06:04:48.550 回答
0

您好,使用此代码删除$productId具有 customerid 的客户的 productid 的产品$customerId

$itemCollection = Mage::getModel('wishlist/item')->getCollection()
    ->addCustomerIdFilter($customerId);
foreach($itemCollection as $item) {
    if($item->getProduct()->getId() == $productId){
        $item->delete();
    }
}
于 2014-10-31T10:26:14.447 回答