-->

Friday, December 21, 2012

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,



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

$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..

Kindly Bookmark this Post using your favorite Bookmarking service:
Technorati Digg This Stumble Stumble Facebook Twitter

0 comments :

Post a Comment

Related Posts Plugin for WordPress, Blogger...
 

Recent Posts

Recent Comments

| Learn Web Tips - A Blog For Learn And Discuss Web Tricks And Tips © 2012. All Rights Reserved | Template Style by My Blogger Tricks .com | Design by Brian Gardner | | Back To Top |