Tutorialsplane

Magento 2 get Cart Items


Magento 2 get Cart Items from session– While working with cart we sometimes need to get quote all items. It is very simple to get all visible items from quote/cart object. Here in this tutorial, we are going to explain how you can get the cart items programmatically.


Magento 2 get Cart Items From Session | Quote Items Example

You can use object manager to get the quote session and items.

Get All Items

This will give all items.

Get All Items From Quote Session Example:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');

$allItems = $cart->getQuote()->getAllItems();
        foreach ($allItems as $item) {
            echo $item->getItemId();
            echo $item->getName();
            echo $item->getSku();
        }

Get All Visible Items

This will give all visible items that can be directly displayed.

Get All Items From Quote Session Example:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');

$allVisibleItems = $cart->getQuote()->getAllVisibleItems();
        foreach ($allVisibleItems as $item) {
            echo $item->getItemId();
            echo $item->getName();
            echo $item->getSku();
        }