2

Below is my code but I can't fetch order items details,just I need order items from sales_order_grid_collection object,I also try getAllItems() but it does not work

This is my config.xml file

<sales_order_grid_collection_load_before>
                <observers>
                    <package_module_sales_order_grid_collection_load_before>
                        <class>marketplace/observer</class>
                        <method>customOrderGridCollection</method>
                    </package_module_sales_order_grid_collection_load_before>
                </observers>
            </sales_order_grid_collection_load_before>

this is observer.php

public function customOrderGridCollection(Varien_Event_Observer $observer)
 {  
            $orderGridCollection = $observer->getOrderGridCollection();
            // $collection = $observer->getEvent()->getOrderGridCollection();
   foreach ($orderGridCollection as $order) 
   {
    foreach ($order->getAllItems() as $item) //$order->getAllItems() not work 
    {
        // $item->getData('product_id') not work 

    }
   }

 }
1

As you have using Collection load_before event and using nested foreach loop that it have creating lot of time taken for execute the observer code.That make huge issue

My suggestion to avoid to multiple times order and order item load. Use this process getting the item data.

  • First get all order ids using ->getAllIds()

$orderGridCollection->getAllIds();

  • Then filter order item collection Mage::getResourceModel('sales/order_item_collection') by order ids

$itemCollection=Mage::getResourceModel('sales/order_item_collection')->addFieldToFilter('order_id',array('in'=>$orderGridCollection->getAllIds()));

If you want all records of specific one column value then add getColumnValues($colName). Just like

$itemCollection->getColumnValues('product_id')

  • Amit, I have one doubt. Collection would be available in load_before event ? – Rajeev K Tomy Oct 9 '15 at 5:23
  • rajeev.. i will get back to you shortly.. – Amit Bera Oct 9 '15 at 7:41
1

May be the event that you are listening is wrong. I think you should use sales_order_grid_collection_load_after in this case. Because sales_order_grid_collection_load_before is an event is dispatched just before the collection get loaded. That means the real collection is not available in that event.

If you can't find the order items in sales_order_grid_collection_load_after, then another solution that comes to my mind is use the event sales_order_grid_collection_load_before and then join the table sales_flat_order or sales_flat_order_item into the collection. You can do join somewhat like this.

$collection->join(
    'sales/order_item', 'order_id=entity_id',
    array('name'=>'name', 'sku' =>'sku', 'qty_ordered'=>'qty_ordered' ),
    null, 'left'
 );

Then use sales_order_grid_collection_load_after to grab the collection again.

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.