Using the sales_order_place_after event in my custom Observer.
I have the quote_item_id value (example 324).
How to get the corresponding item_id value from the sales_flat_order_item table?

I am able to successfully use getCollection method, but it returns the order item as an array.
($sales_order_item_id in below example)

How to get the string value? Eg 224 in example table
(in an efficient manner)

class Custom_Model_Observer {
 public function booking(Varien_Event_Observer $observer) {
    $OrderId = $observer->getEvent()->getOrder()->getId(); //order entity id        
    $QuoteObject = Mage::getSingleton('checkout/session')->getQuote(); //quote object

    foreach ($QuoteObject->getAllVisibleItems() as $item) {

      //get the quote item id - in example $quote_itemId = 324
      $quote_itemId = $item->getData('item_id');

      //get data from sales_flat_order_item table
      $sales_order_item_id = Mage::getModel('sales/order_item')
       ->addFieldToFilter('quote_item_id', $quote_itemId)
     //do more stuff here


Example sales_flat_order_item table:

sales_flat_order_item table

  • $sales_order_item_id = Mage::getModel('sales/order_item')->getCollection()->addFieldToFilter('quote_item_id', $quote_itemId)->getItemId(); Apr 22 '19 at 18:44
  • Did already try that, but not working. Code stops dead, no log error, no exception.
    – Dan
    Apr 22 '19 at 19:08

Normally you can iterate through the order item collection and get both - the item_id and the quote_item_id in one loop, like this

$order = $observer->getEvent()->getOrder();
foreach ($order->getAllVisibleItems() as $orderItem){

But I'm not sure that there already exist the item_id in the event you are using, sales_order_place_after. That id is available only after the order and the order items are saved in the database. The right event for accessing that data would be sales_order_save_after.

  • in your example (using sales_order_save_after event), does $orderItem->getId(); get the order id, or the order item id? I was able to extract the 'item_id' by using $orderitem_id_value = $sales_order_item_id[0]['item_id']; from the array, but this seems so inefficient. I will test your suggestion.
    – Dan
    Apr 22 '19 at 18:59
  • $orderItem->getId() is the order item id. $order->getId() would be the order id.
    – HelgeB
    Apr 22 '19 at 19:03
  • I'm not able to use both foreach without refactoring code (too much effort, too little time), and as my method is working, I will stick to it. Will up vote your useful suggestion. Let me know if you think deserves more credit etc.
    – Dan
    Apr 23 '19 at 17:55

can also use getColumnValues($colName)
this also returns array, within which value would be $sales_order_item_id[0]

$sales_order_item_id = Mage::getModel('sales/order_item')
       ->addFieldToFilter('quote_item_id', $quote_itemId)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.