Home    Articles

 

How to get all items from cart in magento

 

In order to get all items from customer's cart and to display the cart content nicely, first you have to get the items like that:

Store the items in a simple array

<?php
$session = Mage::getSingleton('checkout/session');
$cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
$_Products = array();
$i=0;
foreach ($session->getQuote()->getAllItems() as $item) 
{
	$the_product = Mage::getModel('catalog/product')->load($item->getProductId());
	
	$_Products[$i]['name'] =  $the_product->getName();
	$_Products[$i]['sku'] = $the_product->getSku();
	$_Products[$i]['qty'] = $item->getQty();
	$_Products[$i]['description'] = $the_product->getDescription();
	$_Products[$i]['small_image'] = (string)Mage::helper('catalog/image')->init($the_product, 'small_image')->resize(80);
	$_Products[$i]['price'] = $the_product->getFinalPrice();//Mage::helper('tax')->getPrice($the_product, $the_product->getFinalPrice());
	$_Products[$i]['image_url'] = $the_product->getImageUrl();
	$i++;
}
echo '<pre/>';print_r($_Products);
?>

Now you can use the $_Products array to make a nice dropdown and list the products as you want.

Enjoy.

by