2

I have items in cart. But it returns empty array when fetching them. I don't know what is the problem. I used following code for getting the number of items in cart and it gives 0.But in my site I have items in the cart.

$totalItems = Mage::helper('checkout/cart')->getItemsCount();
echo $totalItems;

$session= Mage::getSingleton('checkout/session');
foreach($session->getQuote()->getAllVisibleItems() as $item)
{
   $productid = $item->getProductId();
   $productsku = $item->getSku();
   $productname = $item->getName();
   $productqty = $item->getQty();
   echo $productid;
}

I hope someone will help on this issue.

10
  • checkout will give you current session cart item..
    – Amit Bera
    Jun 26, 2015 at 8:07
  • yes.. In my site i have items in cart..
    – Gayathri
    Jun 26, 2015 at 8:08
  • please try this cdoe: Mage::getSingleton('checkout/session')->getQuote()->getItemsCount()
    – Amit Bera
    Jun 26, 2015 at 8:15
  • it gives me a blank page..@AmitBera
    – Gayathri
    Jun 26, 2015 at 8:18
  • try with checout car single to object .. Mage::getSingleton('checkout/cart')->getCart()->getItemsCount() ..
    – Amit Bera
    Jun 26, 2015 at 8:20

4 Answers 4

2
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();

  foreach ($items as $item) {
       $itemId = $item->getItemId();
       $itemCount=$item->getName();
  }

Try this code

2
  • I also tried this code...
    – Gayathri
    Jun 26, 2015 at 8:16
  • how do we get only visible (parent) items?
    – snh_nl
    Aug 13, 2020 at 11:11
2

The problem is that someone has changed the database query (to fetch cart items). For example: added GROUP BY

and then see the class

Mage_Catalog_Model_Resource_Product_Collection::_getSelectCountSql(), _buildClearSelect()

can be solved as follows:

copy file from

app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php

to

app/code/local/Mage/Catalog/Model/Resource/Product/Collection.php

and add to function _buildClearSelect before return $select;:

$select->reset(Zend_Db_Select::GROUP);
1
  • Thanks @Neklo.com It's really an informative... I have changed the code as you post..But same blankpage error
    – Gayathri
    Jun 26, 2015 at 10:41
0

Try this :

$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item){
    $productId = $item->getProductId();
}
1
  • Thanks @Anshu Mishra... I also checked this code...gives same empty array
    – Gayathri
    Jun 26, 2015 at 8:03
0

try this code:- To get your cart object (in session) :

$quote = Mage::getSingleton('checkout/session')->getQuote();

Then, to get the list of items in the cart :

$cartItems = $quote->getAllVisibleItems();

Then, to get the count for each item :

foreach ($cartItems as $item)
{
echo $item->getQty();
}
1
  • Thanks @Ashvin Monpara actually i am getting blank page...I used this code too...
    – Gayathri
    Jun 26, 2015 at 10:00

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.