我正在使用Magento的观察者钩子来挂钩订单保存过程.在订单设置完成并保存后,我想向Web服务发送消息.
但是我注意到Web服务经常收到重复的订单商品.
这是我的代码的简化版本,我知道它会出现问题:
<?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?
}
}
?>
解决方法:
如果订单包含可配置产品,则$order-> getAllItems();
将包含父产品和子产品,导致此产品类型的双元素数.使用$order-> getAllVisibleItems()更安全