Magento 2: Get all shopping cart items, subtotal, grand total, billing & shipping address
This article shows how to get shopping cart items/products, subtotal and grand total of cart, and shipping and billing address entered while doing checkout in Magento 2.
I will be using both Dependency Injection (DI) and Object Manager in the below example code.
Using Object Manager
– Get products id, name, price, quantity, etc. present in your cart.
– Get number of items in cart and total quantity in cart.
– Get base total price and grand total price of items in cart.
– Get billing and shipping addresses entered during checkout.
Get all items information in cart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); // retrieve quote items collection $itemsCollection = $cart->getQuote()->getItemsCollection(); // get array of all items what can be display directly $itemsVisible = $cart->getQuote()->getAllVisibleItems(); // retrieve quote items array $items = $cart->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 />"; } |
Get total items and total quantity in cart
1 2 3 4 5 | $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); $totalItems = $cart->getQuote()->getItemsCount(); $totalQuantity = $cart->getQuote()->getItemsQty(); |
Get subtotal and grand total price of cart
1 2 3 4 5 | $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); $subTotal = $cart->getQuote()->getSubtotal(); $grandTotal = $cart->getQuote()->getGrandTotal(); |
Get billing and shipping addresses
1 2 3 4 5 6 7 8 | $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); $billingAddress = $cart->getQuote()->getBillingAddress(); echo '<pre>'; print_r($billingAddress->getData()); echo '</pre>'; $shippingAddress = $cart->getQuote()->getShippingAddress(); echo '<pre>'; print_r($shippingAddress->getData()); echo '</pre>'; |
Using Dependency Injection (DI)
In the above code in Object Manager, I have used object of class \Magento\Checkout\Model\Cart to fetch shopping cart items and totals. But, we can also use the object of class \Magento\Checkout\Model\Session.
Here is your block class code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | namespace YourCompany\YourModule\Block; class YourModule extends \Magento\Framework\View\Element\Template { protected $_cart; protected $_checkoutSession; public function __construct( \Magento\Backend\Block\Template\Context $context, ... \Magento\Checkout\Model\Cart $cart, \Magento\Checkout\Model\Session $checkoutSession, array $data = [] ) { $this->_cart = $cart; $this->_checkoutSession = $checkoutSession; parent::__construct($context, $data); } public function getCart() { return $this->_cart; } public function getCheckoutSession() { return $this->_checkoutSession; } } |
Your .phtml template code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | // Get all items in cart $quote = $block->getCheckoutSession()->getQuote(); $items = $quote->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 />"; } // Get total items and total quantity in cart $totalItems = $quote->getItemsCount(); $totalQuantity = $quote->getItemsQty(); //Get subtotal and grand total price of cart $subTotal = $quote->getSubtotal(); $grandTotal = $quote->getGrandTotal(); //Get billing and shipping addresses $billingAddress = $quote->getBillingAddress(); $shippingAddress = $quote->getShippingAddress(); |
Hope this helps. Thanks.





Mukesh Chapagain is a graduate of Kathmandu University (Dhulikhel, Nepal) from where he holds a Masters degree in Computer Engineering. Mukesh is a passionate web developer who has keen interest in open source technologies, programming & blogging.