我正在使用 Magento 的观察者 Hook 来 Hook 订单保存过程。我想在订单设置为完成并保存后向 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()
关于php - 在 Magento 的 sales_order_save_commit_after Hook 中,为什么我的订单中出现重复的商品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14526728/