Question

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')
       ->getCollection()
       ->addFieldToFilter('quote_item_id', $quote_itemId)
       ->getData('item_id');
     //do more stuff here

    }
  }
}

Example sales_flat_order_item table:

sales_flat_order_item table

Was it helpful?

Solution 2

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')
       ->getCollection()
       ->addFieldToFilter('quote_item_id', $quote_itemId)
       ->getColumnValues('item_id');

https://magento2.atlassian.net/wiki/spaces/m1wiki/pages/14024829/Using+Magento+1.x+collections

OTHER TIPS

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){
    //quote_item_id
    $orderItem->getQuoteItemId();
    //item_id
    $orderItem->getId();
}

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top