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


  • why you want to refresh page?? – Amit Bera Aug 25 '14 at 12:19

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

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:

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

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.