Magento – How to Get Cart Item Details in Event Observer

checkoutevent-observermagento-1.9ordersproducts

I'm using an event observer which fires when user clicks on the 'Place Order' button in checkout flow. I want to capture the product details which are in the cart. Actually this moment order object has already created. If I can access order object it is more than enough.

I have tried the followings, but no luck.

I'm using the event checkout_submit_all_after. And that event executes the following function in the observer.

public function galileo_booking_request(Varien_Event_Observer $observer)
    {
       // $product = $observer->getData('product');// returns null object
      //   $order = $observer->getEvent()->getOrder();// returns null object
           $request = $observer->getEvent()->getRequest()->getParams();// returns null object
           Mage::log($request['product'],null,'eventoberver.log',true);
    }

Best Answer

The observer receives as a parameter the order (or orders for multishipping) and the quote. Here is how you can get them

public function galileo_booking_request(Varien_Event_Observer $observer) {
    $order = $observer->getEvent()->getOrder(); //I don't know why this returns null for you. It shouldn't
    //or for multishipping
    //$order = $observer->getEvent()->getOrders(); //you should get an array of orders
    //for the quote
    $quote = $observer->getEvent()->getQuote();
}

If it doesn't work for you for the order, try with the quote. After getting the quote object your can loop through the items.

foreach ($quote->getAllItems() as $item) {
    $product = $item->getProduct();//if you need it
    //your magic here.
}
Related Topic