balanv
balanv

Reputation: 10888

How do I get items in a Magento shopping cart using models?

Is there any code with which I could fetch items added to the shopping cart and their count from Magento using any models or helpers?

Upvotes: 12

Views: 49860

Answers (4)

Vincent Marmiesse
Vincent Marmiesse

Reputation: 176

You can also do

$cartItems = Mage::getModel("checkout/cart")->getItems();
foreach($cartItems as $item) {
    // Do what you want
}

Upvotes: 2

balanv
balanv

Reputation: 10888

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

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

This code will also help...

Upvotes: 3

Drew Hunter
Drew Hunter

Reputation: 10114

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

$items = $quote->getAllVisibleItems();

foreach($items as $cartItem) {
    echo $cartItem->getQty();
}

To get the total count in the cart you can use:

 Mage::getSingleton('checkout/cart')->getSummaryQty();

Upvotes: 7

Hervé Guétin
Hervé Guétin

Reputation: 4392

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();
}

Upvotes: 45

Related Questions