2012-08-03 30 views
2

在Magento的,我想刪除或刪除的心願列表項目當前登錄的用戶。 目前我通過啓用複選框選擇心願項目,然後通過使用法師:: getModel(「心願/項目」)將其刪除 - >負載($ ID) - >刪除()。我使用的代碼片段粘貼在下面。當我點擊提交按鈕後,項目將被刪除,但是當我再次刷新頁面的效果纔可見。問題是我需要強行刷新頁面才能看到刪除的項目。 有人建議我任何合適的解決方案。這將不勝感激。如何刪除心願藏品在Magento

注意:當選擇了複選框,願望清單項目ID被分配給它的值字段爲:

值= $用品 - > getWishlistItemId()

表單提交之後,下面的代碼執行

if(isset($_POST['wishlist'])) // wishlist is name of check box. 
    { 
     $checkboxes = $_POST['wishlist']; 
    foreach ($checkboxes as $id):  
     $bulk = $_COOKIE["bulkaction"]; 
     if($bulk == "Remove"): 
     Mage::getModel('wishlist/item')->load($id)->delete(); // $id contains the wishlist item id. 
     $url = $this->getBaseUrl().'wishlist'; 
     header("refresh:0.0000000000001;", $url); 
     endif; 
    endforeach; 
} 

回答

1

如果我理解正確的,要刪除與特定用戶相關聯的所有心願項目? ...

$customerId = 1; // Replace with the customer id you are targetting 

$itemCollection = Mage::getModel('wishlist/item')->getCollection() 
    ->addCustomerIdFilter($customerId); 

foreach($itemCollection as $item) { 
    $item->delete(); 
} 
+5

澄清一點的:這似乎是它會從數據庫中刪除產品,而不是僅僅從集合對象中刪除它。我相信,從數據庫中刪除該項目將是錯誤的意圖。 – 2013-05-31 17:42:32

0

德魯的回答肯定會刪除數據庫中的項目。 不過,如果你想從刪除項目「當前」集合,你應該使用Varien_Data_Collection :: removeItemByKey()

$items = $this->getItems(); 
foreach ($items as $key => $item) { 
    if ($condition) $items->removeDataByKey($key); 
} 

但請注意,有時(當集合中的代碼不同的地方單獨訪問)它可能看起來就像它不工作。