2
votes

In Magento, I want to remove or delete the wishlist items of a currently logged in user. Presently I am selecting the wishlist items by enabling the check boxes, and then delete them by using Mage::getModel('wishlist/item')->load($id)->delete(). The code snippet I have used is pasted below. When I click the submit button, items get deleted but the effect is only visible when I refresh the page again. The problem is I need to forcibly refresh the page so to see the items deleted. Can anybody suggest me any suitable solution. That will be appreciated.

Note: When check box is selected, wishlist-item id is assigned to its value field as:

value= $item->getWishlistItemId()

After form submission, following code executes

    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;
}  
3

3 Answers

1
votes

If i understand correctly, you want to delete all wishlist items associated with a particular user? ...

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

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

foreach($itemCollection as $item) {
    $item->delete();
}
0
votes

Drew's answer would definitely delete items from database. However if you would like to remove items from "current" collection you should use Varien_Data_Collection::removeItemByKey()

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

But please note that sometimes (when collection is accessed separately in different places in code) it might look like it doesn't work.

0
votes

You can use

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