Get last order details from checkout session in Magento


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. // 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.
  4. $orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
  5. $order = Mage::getSingleton('sales/order')->loadByIncrementId($orderId);
  6.  
  7. // To get some basic order details, subtotal, shipping cost, discount, tax and grand total.
  8. echo "order subtotal: ".$order->getSubtotal()."<br>";
  9. echo "shipping: ".$order->getShippingAmount()."<br>";
  10. echo "discount: ".$order->getDiscountAmount()."<br>";
  11. echo "tax: ".$order->getTaxAmount()."<br>";
  12. echo "grand total".$order->getGrandTotal()."<br><br><br>";
  13.  
  14. // 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
  15. echo "Complete Order detail:<br>".print_r($order->debug(),true)."<br>";
  16.  
  17. // To get the order details for each item in the order, the order item detail is stored in the table sales_flat_order_item
  18. $orderItems = array();
  19. foreach($order->getItemsCollection() as $item)
  20. {
  21. //$product = Mage::getModel('catalog/product')->load($item->getProductId());
  22. $row=array();
  23. $row['sku'] = $item->getSku();
  24. $row['original_price'] = $item->getOriginalPrice();
  25. $row['price'] = $item->getPrice();
  26. $row['qty_ordered']= (int)$item->getQtyOrdered();
  27. $row['subtotal']= $item->getSubtotal();
  28. $row['tax_amount']= $item->getTaxAmount();
  29. $row['tax_percent']= $item->getTaxPercent();
  30. $row['discount_amount']= $item->getDiscountAmount();
  31. $row['row_total']= $item->getRowTotal();
  32. $orderItems[]=$row;
  33. }
  34. echo "All items in the order:<br>".print_r($orderItems,true)."<br><br><br>";

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.