# How to get order totals?

I need order totals detail like in screen shot below

I tried below code but it is not working.

    $totals =$order->getTotals();
foreach ($totals as$_total) {
echo $_total->getCode() . ' => ' .$_total->getValue() . '<br />';
}


How can I achieve this?

• You can try this: echo $this->helper('checkout')->formatPrice($this->getTotal()->getValue()) – tecjam May 19 '15 at 11:50
• Order has already made. I need order totals like in admin. What class is used to create $this in$this->helper('checkout')->formatPrice($this->getTotal()->getValue()) – piyush_systematix May 19 '15 at 11:57 • Ok, a little more info would have saved that comment. So you want to load an order in the backend, how do you want to do this? via an order_id or customer_id or how? – tecjam May 19 '15 at 12:08 • I have order id, customer id and invoice id. I can use solution having any one of the ids. – piyush_systematix May 19 '15 at 12:16 ## 2 Answers If you have the order id: $order = Mage::getModel("sales/order")->load($orderId); // get order$order->getGrandTotal(); // get order total


Magento doesn't give us an out-of-the-box solution for it, but you can make your own class, copying much of the same logic that the PDF renderer class uses to collect its totals. Instead of putting them in a PDF, we will put them in an orderly array.

Copy the class definition below into your own helper class, then call the function like so, passing it an invoice:

Mage::helper('my_module/totalcollector')->getTotals($invoiceObject);  The function outputs an array that looks like this: Array ( [0] => Array ( [amount] =>$40.00
[label] => Subtotal:
[font_size] => 7
)
[1] => Array (
[amount] => -$25.00 [label] => Discount: [font_size] => 7 ) [2] => Array ( [amount] =>$15.50
[label] => Shipping & Handling:
[font_size] => 7
)
[3] => Array (
[amount] => $30.50 [label] => Grand Total: [font_size] => 8 ) )  Here's the helper class definition: <?php // Most of this code was copied from Mage_Sales_Model_Order_Pdf_Abstract and then slightly modified. class My_Module_Helper_Totalcollector extends Mage_Core_Helper_Abstract { /** * This is an adaptation of the Mage_Sales_Model_Order_Pdf_Abstract::insertTotals function, whose purpose is to "Insert totals to pdf page". * This function will put the totals in a nice array for you to use anywhere you so choose (agonstic of the PDF class.) * The$source parameter can be an invoice or creditmemo (potentially even a shipment).
*
* @param  Mage_Sales_Model_Abstract $source * @return array() */ function getTotals($source){
$order =$source->getOrder();
$totals =$this->_getTotalsList($source);$totalsLines = array();

foreach ($totals as$total) {
$total->setOrder($order)
->setSource($source); if ($total->canDisplay()) {
foreach ($total->getTotalsForDisplay() as$totalData) {
$totalsLines[] =$totalData;
}
}
}

return $totalsLines; } // Code below was selectively copied from Mage_Sales_Model_Order_Pdf_Abstract without any modification (as of Magento v1.9.2.4) var$_defaultTotalModel = 'sales/order_pdf_total_default';
/**
*
* @param  Mage_Sales_Model_Abstract $source * @return array */ function _getTotalsList($source)
{
$totals = Mage::getConfig()->getNode('global/pdf/totals')->asArray(); usort($totals, array($this, '_sortTotalsList'));$totalModels = array();
foreach ($totals as$index => $totalInfo) { if (!empty($totalInfo['model'])) {
$totalModel = Mage::getModel($totalInfo['model']);
if ($totalModel instanceof Mage_Sales_Model_Order_Pdf_Total_Default) {$totalInfo['model'] = $totalModel; } else { Mage::throwException( Mage::helper('sales')->__('PDF total model should extend Mage_Sales_Model_Order_Pdf_Total_Default') ); } } else {$totalModel = Mage::getModel($this->_defaultTotalModel); }$totalModel->setData($totalInfo);$totalModels[] = $totalModel; } return$totalModels;
}

/**
* Sort totals list
*
* @param  array $a * @param array$b
* @return int
*/
protected function _sortTotalsList($a,$b) {
if (!isset($a['sort_order']) || !isset($b['sort_order'])) {
return 0;
}

if ($a['sort_order'] ==$b['sort_order']) {
return 0;
}

return ($a['sort_order'] >$b['sort_order']) ? 1 : -1;
}

}


Note: It looks like your order isn't invoiced yet, so this solution may not exactly help you, but if you can invoice the order first, you can collect the totals using the same method that the PDF printout classes use. Typically, an order placed online will already have an invoice, so I'm posting this here in hopes that it may help others.