Get Shopping Cart Data (Items, Subtotal, Grand Total, Billing & Shipping Address) in Magento 2?

In this blog, I am going to show you how to

  • Get all item information (Products id, Name, Price, Quantity, etc) in your cart.
  • Get the number of items in cart and total quantity in the cart.
  • Get base total price and grand total price of items in the cart.
  • Get billing and shipping addresses entered during checkout.

Here, I have provided the example for both Dependency Injection (DI) and Object Manager to get the shopping cart information in Magento 2.

Using Object Manager

Below the code to get all item information (Products id, Name, Price, Quantity, etc) in your cart.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 
  
// get quote items collection
$itemsCollection = $cart->getQuote()->getItemsCollection();
  
// get array of all items what can be display directly
$itemsVisible = $cart->getQuote()->getAllVisibleItems();
  
// get 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 />"; 
}

Below the code to get the number of items in cart and total quantity in the cart.

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

Below the code to get base total price and grand total price of items in the cart.

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

Below the code to get billing and shipping addresses entered during checkout.

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

Here is the example code to get the shopping cart information using dependency injection.

In this, we might need to inject the object of \Magento\Checkout\Model\CartFactory class in the constructor of our module’s block class and access it from the view ( .phtml ) file. We can also use \Magento\Checkout\Model\SessionFactory class to get shopping cart details.

Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php

<?php
namespace YourCompanyName\YourModuleName\Block;
class YourCustomBlock extends \Magento\Framework\View\Element\Template
{ 
    protected $_cart;
 
    protected $_checkoutSession; 
  
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Checkout\Model\CartFactory $cart,
        \Magento\Checkout\Model\SessionFactory $checkoutSession,
        array $data = []
    ) {
        $this->_cart = $cart;
        $this->_checkoutSession = $checkoutSession;
  
        parent::__construct($context, $data);
    }
  
    public function getCart() { 
        return $this->_cart->create();
    }
  
    public function getCheckoutSession() {
        return $this->_checkoutSession->create();
    }
}

Now, we can use the functions in our view ( .phtml ) file as follows.

// Get all items in cart
$quote = $block->getCheckoutSession()->getQuote();
$items = $quote->getAllItems();
  
foreach($items as $item) {
    echo 'Product 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 /><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.

Leave a Reply

Your email address will not be published. Required fields are marked *