I’m using Magento’s observer hooks to hook into the order saving process. I’d like to send a web service a message after an order is set to complete and saved.
However I’ve noticed that the web service often receives duplicate order items.
Here is a simplified version of my code which I know exhibts the problem:
<?php
class Name_Modulename_Model_Observer {
public function sales_order_save_commit_after($observer) {
// Gets the order which is being saved.
$order = $observer->getOrder();
$status = $order->getStatus();
if($status != "complete") {
continue;
}
// PROBLEM - The number of Mage_Sales_Model_Order_Item
// in this array sometimes does not correspond with the
// The number of items in the basket.
$items = $order->getAllItems();
$itemsInOrder = array();
foreach($items as $item) {
$product = $item->getProduct();
$itemsInOrder[] = $product->name;
}
// At the end of the loop $itemsInOrder can contain
// multiple name entries for the same line item. Why is this?
}
}
?>
If an order contains configurable products, the collection returned by
$order->getAllItems();will contain parent and child products resulting in double element count for this product type. It is safer to use
$order->getAllVisibleItems()