Get Order and Item Details

Posted by in Magento

Since Magento is an e-commerce platform, at some point you’re going to have to deal with customer’s orders. At time of writing, Magento doesn’t allow you to really edit an order that’s already placed (such as removing and adding items purchased) without using a third-party extension or fiddling directly in the database. Sure, you can cancel the order and recreate it with the same increment ID, but it’s still actually a whole new order. As for now, you can change an order’s status/state, and add comments and invoices, but anything further than that would require a more invasive approach.

For now though, we’ll focus on getting the details for an order:

$order = Mage::getModel('sales/order')->load($entityId);

This is how you get the general order details of a specific order. For this example, you should use the entity ID of the order (usually 6 digits) rather than the increment ID (the order ID that’s shown to users and customers on the website). Alternatively, if you’d rather use the increment ID, you can do this:

$order = Mage::getModel('sales/order')->loadByIncrementId($incrementId);

Now that we have an order, you can take a peek at what it holds:

print_r($order->getData());

This only shows you the details held in the order model though. It doesn’t show you details of items… or does it?

$items = $order->getAllItems();
foreach ( $items as $item ) {
    $product = Mage::getModel('catalog/product')->load($item->getProductId());
    print_r($product->getData());
}

This is how you can get, and show, all the details of all the items in your chosen order. Notice that, in the example, it gets all the items for the order and then loads the product model for each one based on the item’s product_id. This is because getAllItems() gets the products from the sales/order_items collection rather than the catalog/product collection. If there are order/item-related details that you want, such as how much the customer paid for the item, quantities, and applied discounts, you needn’t load the product model separately.