1

How to delete order item from order object? For example:

$order = Mage::getModel('sales/order')->load(1);

foreach($order->getAllItems() as $item) {
    $order->removeByItemId($item->getId());
}
3

This is more of a hack. Warning: you could accidentally delete items if you're not careful.

The getAllItems method filters items out of the item collection and returns it. In order to filter items out of the getAllItems array you simply need to mark the desired items in the collection as deleted:

foreach($order->getAllItems() as $item) {
    $item->isDeleted(true);
}

Subsequent calls to getAllItems will return items not marked as deleted.

Now, where things get hairy is that if you call save on that item, it will delete it. This is dangerous.

So, what I suggest instead, is that you build your own collection with only the data that you know that you want. For instance:

$collection = new Varien_Data_Collection();

And then populate it:

foreach($order->getAllItems() as $item) {
    //some condition
    $collection->addItem($item);
}

Now your new collection only contains the data that you want, and you can hand that around.

  • Thanks for the answer and the question edit. However, I would like just to remove the item from the object not really delete it. Maybe I did not put my question right. – LDusan Feb 28 '14 at 18:27
  • Why would you remove it from the object? What is the end goal? – philwinkle Feb 28 '14 at 18:28
  • I am making cloned order objects in order to pass them to mailer class. Now some of these mails that should be sent need to contain some items while others don't. I want to loop through order items and make several cloned order objects with specific items in them. – LDusan Feb 28 '14 at 18:31
  • If I can post a sub question (regarding this approach), how to remove all items from order? – LDusan Feb 28 '14 at 19:01
  • On any collection you can remove items from the collection by calling removeItemByKey but the trick is knowing what the key is... – philwinkle Feb 28 '14 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.