How to get all items in cart currently?

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

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

Solutions

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
}
$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

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.

Similar questions

Shopping Cart Rule: Buy 2 items, get 5% on 2nd; Buy 3 items, get 5% on 2nd + 10% on 3rd
I am trying to set up a shopping cart rule like this, but I am not able to think of the best solution. This is the scenario: I have a product X. If user buys 2 quantities of X, he should get 5% on the second X. If user buys 3 quantities of X, he should get 5% on the second X and 10% on the third X. Is there an easy way to do this via code or extens...
Magento 2 : How do i ONLY display the number currently in cart?
I am trying to display the number of items currently in my cart, however not getting any result. In magento 1 we just just add the following to our phtml file and it will just echo the no. of items in cart. This of course does not work in in magento 2 (version 2.0.2). Within my custom html file. I just need to echo just the number, just like you ge...
The amount of people that currently have one product in their cart?
How to display how many times a product has been added to cart Including all users.
Magento 2 new fieldset/field in cart price rule show unwanted Currently Active Tab
This is in my custom module xml sales_rule_form.xml: AND the it display in the backend like this: See highlighted blue text 'Currently Active' tracking this down it's coming from Native Magento xml at: ../vendor/magento/module-sales-rule/view/adminhtml/ui_component/sales_rule_form.xml and the xml is: How, What ? anyone experience this?
The shopping cart items counter doesn't display 0 when there are no items in the cart
On my website header I have a shopping cart icon with a badge that display the number of the items in the cart. When you enter in the website, it should display 0, as you have no item in the cart. However the badge is empty until you add a item in the cart. But if you later remove all items than it does display 0. This is my code: How to modify it ...
Magento Cart:Checking if the items in cart are reordered items
It is possible to check if the items in the cart are reordered items or not.When the store is changed by the customers the cart items of previous store is removed. When the reorder is done from the user area, I have redirected to the respective stores from where the items was purchased.So the problem is that, the reordered items are also removed fr...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.