Getting Ordered Items and their Detail from Order ID in Magento

Here is a small snippet of code, yet useful, to get ordered items and its details using the order id. Sometimes we need to access product Id, Product SKU, product name and the categories of those products by using the order number,Please use below code for this,

require ‘app/Mage.php’;

$order_id = 1433325;
$order = Mage::getModel(‘sales/order’)->loadByIncrementId($order_id);

// 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 “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/>”;

Hope it is very clear from this code itself..

About Shino

Shino, the author of this blog is a 28 year old master graduate in Computer Science, a professional programmer and web developer. is his humble venture to share whatever he knows and learns about programming,technology and web world.

Leave a Reply