Magento Order Code Snippets

Some useful Magento code snippets for order

Posted on April 29, 2016 in Magento

Getting order details from the order id, it’s an easy task in Magento.

If you have an order id in hand, call the below function for the order object.

$order = Mage::getSingleton('sales/order')->loadByIncrementId($orderId);

Or you wan to get the order details from the checkout session, you can use the below function.

$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order = Mage::getSingleton('sales/order')->loadByIncrementId($orderId);

To get some basic order details use below code snippets

echo "Subtotal: ". $order->getSubtotal();
echo "Shipping Amount: " . $order->getShippingAmount();
echo "Discount: ". $order->getDiscountAmount();
echo "Tax Amount: " . $order->getTaxAmount();
echo "Grand Total: ". $order->getGrandTotal();

Getting all the products from order object

foreach($order->getAllVisibleItems() as $item) {
    echo $item->getName(); // product name
    echo $item->getSku(); //product sku
    echo $item->getPrice(); //product price
    echo $item->getQytOrdered(); // ordered qty

comments powered by Disqus