45

I want to get a collection of all the items in cart currently.

How can I do this ? Any suggestions will be appreciated.

61
$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $item) {
    $productName = $item->getProduct()->getName();
    $productPrice = $item->getProduct()->getPrice();
}

in $cart you got all collection of cart item and if you want to get product id,name you can get from using foreach loop

  • 6
    it works but it displays all of the products including the products which belongs to configurable products. To display all of products except the ones that i mentioned you must use getAllVisibleItems() – Attila Naghi Dec 24 '15 at 13:09
  • How to show quantity of this items? – Marcos Vinicius Jun 14 '16 at 15:48
  • Should you be using getModel ? Shouldn't you be using getSingleton ? – Danny Dev Nz Sep 29 '16 at 23:34
28

I found another solution. The following code works for me.

$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item) {
    $productId = $item->getProductId();     
    // Do something more
}
  • How to show quantity of this items? – Marcos Vinicius Jun 14 '16 at 15:48
  • 1
    Why 27 upvotes for loading products in a loop ....? – sv3n Jan 21 '18 at 16:30
  • This has terrible performance.. you are loading each product in a loop when they are already available via $item->getProduct()... Every time you think about using ->load() in magento you can count on bad performance, sometimes there aren't any other option really but this is not the case here.. – OZZIE Feb 7 '18 at 7:39
  • 1
    @sv3n : Thanks for pointing it out. Modified the code – Sukeshini Feb 13 '18 at 5:18
21

There are several methods that work in a different way:

  1. $items = Mage::getSingleton('checkout/cart')->getQuote()->getItemsCollection();
    

    Returns a quote item collection with all items associated to the current quote.

  2. $items = Mage::getSingleton('checkout/cart')->getItems();
    

    This is a shortcut for the method above, but if there is no quote it returns an empty array, so you cannot rely on getting a collection instance.

  3. $items = Mage::getSingleton('checkout/cart')->getQuote()->getAllItems();
    

    Loads the item collection, then returns an array of all items which are not marked as deleted (i.e. have been removed in the current request)

  4. $items = Mage::getSingleton('checkout/cart')->getQuote()->getAllVisibleItems();
    

    Loads the item collection, then returns an array of all items which are not marked as deleted AND do not have a parent (i.e. you get items for bundled and configurable products but not their associated children). Each array item corresponds to a displayed row in the cart page.

Choose what fits your needs best. In most cases the last method is what you need, but unfortunately Magento only provides it as array and not as collection.


Note that Mage::getSingleton('checkout/cart')->getQuote() and Mage::getSingleton('checkout/session')->getQuote() are interchangable.

1
$items =Mage::getSingleton('checkout/session')->getQuote()->getAllItems();

foreach($items as $item) {
    echo 'ID: '.$item->getProductId().'<br />';
    echo 'Name: '.$item->getName().'<br />';
    echo 'Sku: '.$item->getSku().'<br />';
    echo 'Quantity: '.$item->getQty().'<br />';
    echo 'Price: '.$item->getPrice().'<br />';
    echo "<br />";
}

best way to get the all item in cart

  • Use Mage::getSingleton('checkout/cart')->getQuote()->getAllItems(); – Aman Srivastava Jun 21 '16 at 6:57
  • Why? What's the difference? – LM_Fielding Apr 20 '17 at 9:57
-7

Below code is used for getting cart items

$cart = new Mage_Checkout_Model_Cart();

$cart->init();

foreach ($cart->getItems() as $item) {

   // we can wrap our request in this IF statement

   if (!$item->getParentItemId()) {

   }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.