How to Get the data of shopping cart items, subtotal, grand total, billing & shipping address in Magento 2

When running your store on Magento 2 platform, it is possible to get the data of shopping cart items, subtotal, grand total, billing & shipping address.

When running your store on Magento 2 platform, it is possible to get the data of shopping cart items, subtotal, grand total, billing & shipping address. This guide will be applied to many feature development processes that relate to cart information such as sharing cart or saving cart. Hence, in this article, we will show you the way to get the data by using the commands.

Overview of retrieving the data of shopping cart items, subtotal, grand total, billing & shipping address in Magento 2

You can get information of the cart items on a whole or individually such as subtotal, grand total and billing & shipping address. Here are four approaches:

Get all needed information 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 />";            
}

Get the number of items in cart and total quantity in cart.

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

Get base total price and grand total price of items in cart.

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

Get chosen billing and shipping addresses.

$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>';

That is all things you will use to retrieve the data of shopping cart items, subtotal, grand total, billing & shipping address in Magento 2. Thanks for your reading and please comment below if there is any trouble.

Learn more: Share cart information in Magento 2

Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
get
the
data
of
shopping
cart
items,
subtotal,
grand
total,
billing
&
shipping
address

Sam Thomas
Sam Thomas

CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and an influencer wannabe. He is a big fan of sports and travel, also.

People also searched for

  • magento 2 get the data of shopping cart items
  • magento 2 get cart items
  • magento 2 get cart subtotal
  • magento 2 get cart total
  • get quote items in magento 2
  • magento 2 get quote items
  • get cart items in magento 2
  • magento 2 get quote
  • magento 2 get quote item
  • magento 2 get cart items in phtml
  • magento 2 get cart
  • magento 2 get current quote
  • magento 2 get current cart
  • magento 2 get shipping address from quote
  • magento 2 get cart items count
  • magento 2 get order total
  • magento 2 get cart items from session
  • magento 2 get cart items programmatically
  • get shipping address from quote magento 2
  • get cart items magento 2
  • get cart subtotal magento 2
  • 2.2.x, 2.3.x, 2.4.x