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:

$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 = '';
}

13 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..

  • I have to give free shipping order over and 1000 and zipcode between 600001 to 600010, i am using webshop matrix rate for shipping method, in this case, shopping cart pricing rule not working free shipping, so i have to plan for do custom PHP code for giving free shipping, My condition how can i give condition like subtotal is equal or greated than 1000 and zipcode 600001 and 600010 in /public_html/app/design/frontend/base/default/template/checkout/cart/shipping.phtml

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

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions