Magento sales_order_place_before $ observer getevent null

时间:2015-09-28 09:42:17

标签: php magento

我有以下观察者方法:

public function ModifyOrderECC(Varien_Event_Observer $observer)
{
    //sales_order_place_before
    Mage::log('ECC!! = ModifyOrderECC has been called successfully - event/array-5', null, 'mylog.log');
    $cart = $observer->getEvent()->getCart();
    try{
        if($cart != NULL)
        {
            Mage::log('$cart contains data!', null, 'mylog.log');

            foreach ($cart->getItems() as $item) {
                $getID = $item->getProduct()->getId();
                $productName = $item->getProduct()->getName();
                $productPrice = $item->getProduct()->getPrice();
                $quantity = (string)$item->getProduct()->getQtyOrdered();

                Mage::log('ID: '.$getID, null, 'mylog.log');
                Mage::log('Name: '.$productName, null, 'mylog.log');
                Mage::log('Price: '.$productPrice, null, 'mylog.log');
                Mage::log('Qty: '.$quantity, null, 'mylog.log');
            }
        }else
        {
            Mage::log('sorry Cart is null :(', null, 'mylog.log');
        }

    }catch(Exception $e)
    {
        Mage::log($e->getMessage());
        Mage::log('error = '.$e->getMessage(), null, 'mylog.log');
    }

}

但是getCart事件是null,我也尝试过getquote,它也是null。我需要从事件中获取购物车的内容,以查看其创建的内容。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

根据 sales_order_place_before 事件, 仅提供订单对象并启用直接提供报价对象

您希望此活动中出现quote object,然后您需要从quote id获取Order object,然后使用quote id you can get quote object

 public function ModifyOrderECC(Varien_Event_Observer $observer)
    {
        //sales_order_place_before


        $QuoteID=$observer->getEvent()->getOrder()->getQuoteId();
    $quoteObject=Mage::getModel('sales/quote')->load($QuoteID);
...
    }

答案 1 :(得分:1)

通过查看order类(第1113行),您可以看到此事件为您提供了整个cart实例,而不是Mage_Sales_Model_Order实例。

如果您想检索cart,请执行:

$cart = Mage::getSingleton('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $item) {
    // do something
}