2011-08-22 45 views
2

我已經構建了一個自定義腳本,使用AJAX將項目添加到願望清單並將其刪除。添加產品不是問題,但我無法弄清楚如何刪除一個項目。 Magento版本是1.5.1.0Magento Wishlist - 刪除項目

腳本是/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); 
} 

所以,當我要求腳本「刪除」爲$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」,但我不明白這是什麼。

+0

要在Magento使用Ajax願望清單中移除的產品,我創建了一個自定義的function.For更多詳情,請參閱:http://prasadoturkar.blogspot.in/2013/06/addremove-product-from-wishlist- using.html –

回答

3

我沒有時間來調試任何進入你的代碼錯誤,但使用刪除實體的正常慣例應該工作:

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

作品很有魅力,非常感謝! –

+0

它對我來說工作得很好 –

1

只是看看的Mage_Wishlist_IndexControllerremoveAction。 您必須通過其ID加載心願清單項目,然後您可以撥打delete()方法。

0

我知道這個問題是舊的,但因爲我剛碰到與magento 1.7.0.2相同的問題,我想分享解決方案。

爲了使它工作,你需要使用的方法的updateItem如下

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

而不是

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

您不能使用此方法的項目數量設置爲0,它會自動將其設置爲最小值1,除非使用刪除方法。

0

您好使用此代碼刪除客戶客戶有產品$productId產品$customerId

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