Oct 10, 2011
kalpesh

Magento: Get checkout cart total details | Subtotal/Grandtotal/Discount/Tax

In Magento, if you want to get shopping cart totals details anywhere across the site, you can do so by following piece of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$totalItemsInCart = Mage::helper('checkout/cart')->getItemsCount(); //total items in cart
$totals = Mage::getSingleton('checkout/session')->getQuote()->getTotals(); //Total object
$subtotal = round($totals["subtotal"]->getValue()); //Subtotal value
$grandtotal = round($totals["grand_total"]->getValue()); //Grandtotal value
if(isset($totals['discount']) && $totals['discount']->getValue()) {
    $discount = round($totals['discount']->getValue()); //Discount value if applied
} else {
    $discount = '';
}
if(isset($totals['tax']) && $totals['tax']->getValue()) {
    $tax = round($totals['tax']->getValue()); //Tax value if present
} else {
    $tax = '';
}

12 Comments

  • you should to read it again and think about last sentence. but it`s only my opinion

  • $totalItemsInCart = Mage::helper(‘checkout/cart’)->getItemsCount(); //total items in cart
    $totals = Mage::getSingleton(‘checkout/session’)->getQuote()->getTotals(); //Total object
    $subtotal = round($totals[“subtotal”]->getValue()); //Subtotal value
    $grandtotal = round($totals[“grand_total”]->getValue()); //Grandtotal value
    >if(isset($totals[‘discount’]) && $totals[‘discount’]->getValue()) {
    $discount = round($totals[‘discount’]->getValue()); //Discount value if applied
    } else {
    $discount = ”;
    }

    if(isset($totals[‘tax’]) && $totals[‘tax’]->getValue()) {
    $tax = round($totals[‘tax’]->getValue()); //Tax value if present
    } else {
    $tax = ”;
    }

  • Thanks Kalpesh! It helped!

  • Thank you for the nice snippet kalpesh, your blog is one of my favorite Magento resource. Keep updating with new idea and codes. thanks again 🙂

  • I would like to know whether the code will give the details of tax even afer the transaction is complete and the customer is in thanks page. I need it in thanks page

  • pliiiz how get date of checkout ?

  • 3 Years later and still extremely useful. I’ve spent countless hours trying to find something so simple, and I have found it. Thank you

  • I cant find the page for tax without round-off in my site. am using version 1.9

  • Hello,
    I want to update this subtotal price at checkout onepage via event observer() method, with below code.

    $session = Mage::getSingleton(‘checkout/session’);
    $quote= $session->getQuote();
    $quote->setSubtotal($mycustomvalue);
    $quote->setBaseSubtotal($mycustomvalue);
    $quote->save();

    but its not working. can anyone tell me whats problem?

  • Why are you using round() when dealing with currency?

    Use: Mage::helper(‘core’)->currency($totals[‘subtotal’]->getValue() , true, false);

  • hi we need to calculate the shipping after the discount apply, how to do this.

  • Thanks Kalpesh..

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications