Get last order details from checkout session in Magento

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.

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>";

All the fields and values of an order item:

Array
        (
            [item_id] => 7
            [order_id] => 5
            [parent_item_id] => 
            [quote_item_id] => 19
            [store_id] => 1
            [created_at] => 2014-03-11 02:03:55
            [updated_at] => 2014-03-11 03:06:06
            [product_id] => 52
            [product_type] => simple
            [product_options] => a:1:{s:15:"info_buyRequest";a:3:{s:4:"uenc";s:60:"aHR0cDovL2xvY2FsaG9zdDo4OC9pbmRleC5waHAvZnVybml0dXJlLmh0bWw,";s:7:"product";s:2:"52";s:3:"qty";i:1;}}
            [weight] => 50.0000
            [is_virtual] => 0
            [sku] => 1112
            [name] => Chair
            [description] => 
            [applied_rule_ids] => 
            [additional_data] => 
            [free_shipping] => 0
            [is_qty_decimal] => 0
            [no_discount] => 0
            [qty_backordered] => 
            [qty_canceled] => 1.0000
            [qty_invoiced] => 0.0000
            [qty_ordered] => 1.0000
            [qty_refunded] => 0.0000
            [qty_shipped] => 0.0000
            [base_cost] => 50.0000
            [price] => 129.9900
            [base_price] => 129.9900
            [original_price] => 129.9900
            [base_original_price] => 129.9900
            [tax_percent] => 8.3750
            [tax_amount] => 10.8900
            [base_tax_amount] => 10.8900
            [tax_invoiced] => 0.0000
            [base_tax_invoiced] => 0.0000
            [discount_percent] => 0.0000
            [discount_amount] => 0.0000
            [base_discount_amount] => 0.0000
            [discount_invoiced] => 0.0000
            [base_discount_invoiced] => 0.0000
            [amount_refunded] => 0.0000
            [base_amount_refunded] => 0.0000
            [row_total] => 129.9900
            [base_row_total] => 129.9900
            [row_invoiced] => 0.0000
            [base_row_invoiced] => 0.0000
            [row_weight] => 50.0000
            [gift_message_id] => 
            [gift_message_available] => 
            [base_tax_before_discount] => 
            [tax_before_discount] => 
            [weee_tax_applied] => a:0:{}
            [weee_tax_applied_amount] => 0.0000
            [weee_tax_applied_row_amount] => 0.0000
            [base_weee_tax_applied_amount] => 0.0000
            [base_weee_tax_applied_row_amnt] => 0.0000
            [base_weee_tax_applied_row_amount] => 0.0000
            [weee_tax_disposition] => 0.0000
            [weee_tax_row_disposition] => 0.0000
            [base_weee_tax_disposition] => 0.0000
            [base_weee_tax_row_disposition] => 0.0000
            [ext_order_item_id] => 
            [locked_do_invoice] => 
            [locked_do_ship] => 
            [price_incl_tax] => 140.8800
            [base_price_incl_tax] => 140.8800
            [row_total_incl_tax] => 140.8800
            [base_row_total_incl_tax] => 140.8800
            [hidden_tax_amount] => 
            [base_hidden_tax_amount] => 
            [hidden_tax_invoiced] => 
            [base_hidden_tax_invoiced] => 
            [hidden_tax_refunded] => 
            [base_hidden_tax_refunded] => 
            [is_nominal] => 0
            [tax_canceled] => 10.8900
            [hidden_tax_canceled] => 0.0000
            [tax_refunded] => 
            [base_tax_refunded] => 
            [discount_refunded] => 
            [base_discount_refunded] => 
        )

Search within Codexpedia

Custom Search

Search the entire web

Custom Search