0

When using

$order = Mage::getModel("sales/order")->loadByIncrementId($OrderNo);

I can access product ids by

foreach ($order->getAllItems() as $item)
{
    if ($item->getParentItemId() != NULL) continue;

    echo $item->getProductId();
}

But when I access the order via an event observer this method fails.

$order = $observer->getEvent()->getOrder();

How do I get product ids in this case?

4
  • which event u r using ? Commented Feb 3, 2015 at 13:38
  • I am using sales_order_save_after
    – cads
    Commented Feb 3, 2015 at 13:42
  • try this $ProdustIds=array(); foreach ($order->getAllVisibleItems() as $item) { $ProdustIds[]= $item->getProductId(); }
    – Amit Bera
    Commented Feb 3, 2015 at 13:43
  • Try to use events sales_order_payment_place_end or sales_order_save_commit_after or sales_model_service_quote_submit_success Commented Feb 3, 2015 at 13:44

2 Answers 2

2

if your observer gives you the order object you can get the product ids by using the process below:

$order = $observer->getEvent()->getOrder();

if( $order->getId() ) {
    $ProductIds = array();
    foreach( $order->getAllVisibleItems() as $item ) {
        $ProductIds[] = $item->getProductId();
    }        
}
0

hi here is my xml file :

<global>
   <config>
     <events>
      <sales_order_place_after>
        <observers>
            <order_backendordermgt>
            <class>order_backendordermgt/observer</class>
            <method>updateOrderType</method>
            </order_backendordermgt>
        </observers>
      </sales_order_place_after>
    </events>
</global>

here is my observer :

class Order_Backendordermgt_Model_Observer {    
    public function updateOrderType(Varien_Event_Observer $observer) {
        $order = $observer->getEvent()->getOrder();
        $increment_id = $order->getincrement_id();
        $items = $order->getAllVisibleItems();
        foreach($items as $item){
        echo $item->getName();
        // custom code to update order or any thing
        }
        $orders->save();
    }
}

i think this can help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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