0

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.

if(Mage::getSingleton('checkout/session')->getQuote()->hasItems())
{
}

adding Product to cart code,

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

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

EDIT

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

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

 Mage::getSingleton('checkout/session');

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'). – Manikandan Arunachalam Dec 1 '14 at 5:30
  • try to print Mage::getSingleton('checkout/session')->getQuote() and check whether any quotes exist – Rajeev K Tomy Dec 1 '14 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 . – Manikandan Arunachalam Dec 1 '14 at 5:35
  • did you try Mage::getModel() instead of Mage::getSingleton() ?? – Rajeev K Tomy Dec 1 '14 at 5:39
  • yes i tried that too.But why you asked me to use getModel() any specific reason? – Manikandan Arunachalam Dec 1 '14 at 5:55

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.