php - 在 Magento 的 sales_order_save_commit_after Hook 中,为什么我的订单中出现重复的商品?

标签 php magento

我正在使用 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/

相关文章:

PHP路由: display flash messages in View (Klein)

php - 如何在 laravel 的 if 语句中显示值

带有 memcached 的 magento 全页缓存

magento - Magento 站点转移到 localhost 期间的奇怪问题

PHP + PDF 换行

php - 使用 PHPdoc 记录 PHP 扩展

php - Laravel 4 准备好的语句

php - 如何为图像网格做 CSS

session - Magento Memcached session

magento - 在商店 View 级别以编程方式更改产品属性