I use this code :
// $items = Mage::getModel(\'checkout/cart\')->getQuote()->getAllItems();
$items = Mage::getSingleton(\'checkout/session\')->getQuote(
tl;dr: The answer to this question varies in complexity based on product type (configurable, simple, etc.). This may drive how you setup your catalog and set simple product visibility. If all that you have in your catalog is simple items, this task is very easy. Also: see edit at end.
There are lots of scenarios to consider. For example, if you add a configurable item to the cart, the quote will have two quote items per option: one quote item for the configurable parent product which will contain option quantity information and one quote item which will provide the option's simple product data. Bundle items will have a parent bundle item as well as a simple item for each bundle option.
What this boils down to is that you need to decide how you represent each product type's quantity to the user; for example, do you want to represent the same model of shirt as one line item with all size quantities, or do you want to separate these out for each size (the latter being how Magento renders them in cart / review areas).
I recommend adding your products to the cart, then using this in the frontend to see the quote item data:
'frontend'));
$quote = Mage::helper('checkout/cart')->getCart()->getQuote();
echo count($quote->getItemsCollection());
foreach ($quote->getItemsCollection() as $item){
Zend_Debug::dump($item->debug());
}
I recommend using an install with the sample data and adding different quantities of each product type to the cart. Based on this + the output from the above code, you'll see that you have several approaches and gotchas as you loop through. The confounding scenarios are when you have the following:
Code:
$configurables = array();
// ...then, inside your foreach
if($item->getProductType() === 'configurable'){
if(isset($configurables[$item->getProductId()])){
//add to the other options' quantity
$configurables[$item->getProductId()] += $item->getQty();
}
else {
$configurables[$item->getProductId()] = $item->getQty();
}
}
Code:
$grouped = array();
// ...then, inside your foreach
if($item->getProductType() === 'grouped' || $item->getProductType() === 'simple'){
if(isset($grouped[$item->getProductId()])){
//add to the other options' quantity
$grouped[$item->getProductId()] += $item->getQty();
}
else {
$grouped[$item->getProductId()] = $item->getQty();
}
}
You can see that handling all scenarios is cumbersome. I wish I could tell you that it's easier to do this via resource models, but the fact is that in the database the sales_flat_quote_item_option
table stores quantity info in a column of serialized data, so you'll need to pull things into PHP to get anywhere with them (which is done for you via the resource model).
Sorry this is such a long answer, but you can see that, based on your catalog setup, you're in for some careful consideration and testing to make sure you've got your bases covered.
EDIT: While trying to be thorough, I failed to mention that you should look into the sales_flat_quote_item
table. Depending on your requirements, you can easily use the data in there to quickly, gracefully, and efficiently model the data that you need to achieve your display requirements.