Magento get items in order

Home / Magento / Magento get items in order

Here, I can show you how you can get information about all items in your magento shopping cart based on order id. How to get product details from order id or sometimes need to get product it, product sku, product name , category id, category name. So its easy to get product from order item based on order number.

<?php
require 'app/Mage.php';
Mage::app();

$orderNumber = 100004544;
$order = Mage::getModel('sales/order')->loadByIncrementId($orderNumber);
// get order total value
$orderValue = number_format ($order->getGrandTotal(), 2, '.' , $thousands_sep = '');
// get order item collection
$orderItems = $order->getItemsCollection();

foreach ($orderItems as $item){

	$product_id = $item->product_id;
	$product_sku = $item->sku;
	$product_name = $item->getName();
	$_product = Mage::getModel('catalog/product')->load($product_id);
	$cats = $_product->getCategoryIds();
	$category_id = $cats[0]; // just grab the first id
	$category = Mage::getModel('catalog/category')->load($category_id);
	$category_name = $category->getName();

	echo "orderNumber=".$orderNumber."<br/>";
	echo "orderValue=".$orderValue."<br/>";
	echo "product_name=".$product_name."<br/>";
	echo "product_id=".$product_id."<br/>";
	echo "product_sku=".$product_sku."<br/>";
	echo "category_id=".$category_id."<br/>";
	echo "category_name=".$category_name."<br/><br/>";

}
?>

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *