I have a local module, which adds product to cart.To add to cart i have some conditional check before adding to cart,and i am using getSingleton('checkout/session')->getQuote() object for getting number of items in cart.

My code, initially checks number of items in cart in that session,by below code.


adding Product to cart code,

$cart = Mage::getModel('checkout/cart');
$product = $this->_initProduct();
$related = $this->getRequest()->getParam('related_product');
$cart->addProduct($product, $params);

My Question is , sometimes hasItems() returns false even my cart has products.


I tried logging session_id(), an i found that this session_id changed unexpectedly. That means new session is created , so my cart is empty in this new session.

My Question?

I dont create any session in my code, then why new session is created before the session timeout?

1 Answer 1


You are calling checkout/session model in wrong way. It should be


So your condition will look like

if (Mage::getSingleton('checkout/session')->getQuote()->hasItems()) {
    //do your stuff here
  • its typo,i just updated it.and is that right getting cart object asgetModel('checkout/cart'). Dec 1, 2014 at 5:30
  • try to print Mage::getSingleton('checkout/session')->getQuote() and check whether any quotes exist Dec 1, 2014 at 5:32
  • Thats what i'm doing.when i check it, it shows empty when i tryto add product even cart has item, so to understand the session , i log the session id and it logs new session_id . Dec 1, 2014 at 5:35
  • did you try Mage::getModel() instead of Mage::getSingleton() ?? Dec 1, 2014 at 5:39
  • yes i tried that too.But why you asked me to use getModel() any specific reason? Dec 1, 2014 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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