Home    Articles


Magento get products from order by order id as array


In magento you need to get the products from an order any time.
If you want to get all products from some particular order, you need to get order id first.
Let’s assume that you have already the order id (ex: 23,104,99 not #10000023)
Let’s see the code:

#load the order
$order = Mage::getModel('sales/order')->load($orderId);
#get all items
$items = $order->getAllItems();
$itemcount= count($items);
$data = array();
#loop for all order items
foreach ($items as $itemId => $item)
  $data[$i]['name'] = $item->getName();
  $data[$i]['price'] = $item->getPrice();   
  $data[$i]['sku'] = $item->getSku();
  $data[$i]['id'] = $item->getProductId();
  $data[$i]['qty'] = $item->getQtyToInvoice();
#displaying the array in order to see the products
echo '<pre/>';print_r($data);exit;

Now you have all the products structured in the $data variable. You can use this to send a mail with the last order submited into your magento store or anything else.

Here i will show you how to get the order id by the last increment order id ( this number: #10000023 )

…first time posted on w3bdeveloper.com