Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to refresh the cart page and show a custom block in case my Grand Totals is Negative.

$grandTotal = Mage::helper('checkout/cart')->getQuote()->getGrandTotal();

This prints -$2,269.50, but when I use the below condition, it just don't work.

If ($grandTotal < 0 ){
//do something
//do something else

How can I check if number is less than 0 i.e. negative


share|improve this question
why you want to refresh page?? – Amit Bera Aug 25 '14 at 12:19
up vote 3 down vote accepted

Your problem is that getGrandTotal is returning a string rather then a number. Trying to see if a string is lower then 0 isn't going to work.

You can filter the string and remove anything which isn't a number using

filter_var($grandTotal, FILTER_SANITIZE_NUMBER_INT);

Which in your case would return -226950 which you can then check if it's lower then 0, if you need to do further checks you'll need to handle the way it removes the separator between the pounds and pence.

if(filter_var($price,FILTER_SANITIZE_NUMBER_INT) < 0) {
    // Do something
} else {
    // Do something else
share|improve this answer

If you want the exact amount as well, you can use

$currency_sign = "$";
$actual_amount = str_replace($currency_sign,'',$grandTotal); // $grandTotal is -$2,269.50
if($actual_amount < 0) { //$actual_amount will have the value as -2,269.50
    // Do something
} else {
    // Do something else

all you need is to use the correct currency sign. You can get the current currency sign by the following code:

share|improve this answer
Thanks for the currency code, that's exactly what I was looking for. – hellimac Aug 5 '15 at 10:48

i suggest that instead of use

$grandTotal = Mage::helper('checkout/cart')->getQuote()->getGrandTotal();

please use

$quote = Mage::helper('checkout/cart')->getQuote();
$quoteData= $quote->getData();

it give proper integer/float value.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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