I'm trying to get the subtotal of the cart and subtract it from a value in javascript. This is my code:

 <script type="text/javascript">
function minus_number() {
        var a  = document.querySelector("#spend");
        var first_number = '50';
        var second_number = '<?php echo $this->helper('checkout')->formatPrice(Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal()); ?>';
        var result = parseInt(first_number) - parseInt(second_number);
        a.innerHTML = result;


However it only shows NAN on the page. Any ideas what's wrong?

  • can you do VIEW-SOURCE (ctrl+U) of the code you shown above and paste here ? – Anil Suthar Jul 6 '16 at 12:35

You get NaN here, as second_number variable is not a number (probably, null). That may happen due to you using the wrong part of totals - basically, you may not have grand total here. Try using next line: age::getSingleton('checkout/session')->getQuote()->getSubtotal() The subtotal may be present, while grand total is not yet calculated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.