Here is a small snippet of code, yet useful, to get ordered items and its details. I’ve deviced this code a lot before and posted in Magento Commerce’s Forum as well. But felt like writting it again, so that I can have a quick refrence to it as well. Next thing, I’ve tried a lot to get all orders and their items details by single query, but have not yet come up with a solution. If you have any method of finding order and its details by a single query, then please do response. The code below first needs an order ID as it parameters to give order details.
$order = Mage::getModel('sales/order')->load($order_id);
$items = $order->getAllItems();
$itemcount=count($items);
$name=array();
$unitPrice=array();
$sku=array();
$ids=array();
$qty=array();
foreach ($items as $itemId => $item)
{
$name[] = $item->getName();
$unitPrice[]=$item->getPrice();
$sku[]=$item->getSku();
$ids[]=$item->getProductId();
$qty[]=$item->getQtyToInvoice();
}
Hope this might “just” help somebody in need.
That’s a really useful tip, thanks.
Hi, really useful stuff! I am writing an order export module for magento and this is really good.
Also, just posted a list of order and order item properties on my blog which may be useful to people…
http://www.jimcode.org/2010/10/magento-order-product-properties
how can i print to screen all of this?
It was very helpful to me to get the order details (Item, SKU, quantity).
And i would like to mention the order id is an entity_id of the table sales_flat_order. Actually i first messed up with passing realOrderId (An order increment id) as an argument for
$order_id = $_shipment->getRealOrderId()
$order = Mage::getModel(‘sales/order’)->load($order_id);
[Since am customizing a Shipment email template in email/order/shipment/items.phtml]
but then i ran a raw SQL query to get an Orderid from the real order id and got my shipment details.
SELECT entity_id
FROM {$table_prefix}sales_flat_order
WHERE increment_id = $real_order_id
BTW, Your blog has a pretty useful articles, besides good articles in Magento, i would like to appreciate for this nice work and have subscribed to your blog.
Hope i can learn more from your Blog. Thank you once again !
Thanks Its working for me.
Thank you so much, I could not figure out how to load these!
You saved me hours! Thanks again and keep the posts coming!
Thanks a lot for you code ,but the provided code is working only for simple products.In case of configurable products repetition arises Can you let me know how it should be implemented in case of configurable products and bundle products
Good One, Thank you
thanks for posting this very useful piece of code.
Can you please further guide on how to get the ordered items attributes?
Thank You
There is a problem with the code. If the Item Status of the produtcs is Mixed the getQtyToInvoice() returns 0. I’m using magento 1.4.2. Any fix on this?
@deni: Use $orderItem->getQtyInvoiced();
Thanks all of your articles.
In the above code I have the following errors when execute from external php:
Call to a member function getAllItems()
I’m using 1.4.1.1
Is there any change in the code to work with this version?
Hi,
Thanks for your code, it helped me for magento CE 1.5.1.