Return to Snippet

Revision: 70338
at January 19, 2016 15:09 by toannguyen


Initial Code
<?php

// First we need to the last real order id from the checkout/session and then passing this order id as a parameter to retrieve the order object from sales/order model.
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order = Mage::getSingleton('sales/order')->loadByIncrementId($orderId);

// To get some basic order details, subtotal, shipping cost, discount, tax and grand total.
echo "order subtotal: ".$order->getSubtotal()."<br>";
echo "shipping: ".$order->getShippingAmount()."<br>";
echo "discount: ".$order->getDiscountAmount()."<br>";
echo "tax: ".$order->getTaxAmount()."<br>";
echo "grand total".$order->getGrandTotal()."<br><br><br>";

// To show all the order details, the code below will pull all the order details for this order id from the table sales_flat_order
echo "Complete Order detail:<br>".print_r($order->debug(),true)."<br>";

// To get the order details for each item in the order, the order item detail is stored in the table sales_flat_order_item
$orderItems = array();
foreach($order->getItemsCollection() as $item)
{
    //$product = Mage::getModel('catalog/product')->load($item->getProductId());
    $row=array();
    $row['sku'] = $item->getSku();
    $row['original_price'] = $item->getOriginalPrice();
    $row['price'] = $item->getPrice();
    $row['qty_ordered']= (int)$item->getQtyOrdered();
    $row['subtotal']= $item->getSubtotal();
    $row['tax_amount']= $item->getTaxAmount();
    $row['tax_percent']= $item->getTaxPercent();
    $row['discount_amount']= $item->getDiscountAmount();
    $row['row_total']= $item->getRowTotal();
    $orderItems[]=$row;
}
echo "All items in the order:<br>".print_r($orderItems,true)."<br><br><br>";

Initial URL

                                

Initial Description
The code snippet below can be placed in any template file or anywhere after the Mage is initialized, and there has to be an order placed before.

Initial Title
Get last order details from checkout session in Magento

Initial Tags
magento

Initial Language
PHP