Shopping cart

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. This article 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

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 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 in that.





Enjoyed the tutorial? Spread it to your friends!

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

Brian Tran
Brian Tran
Senior Magento Developer who is passionate about development and love technology

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

Please leave comments if you have any questions, feedbacks.

Module Development

We recommend:

You also may like these Magento 2 Extensions

One Step Checkout

$199
120 reviews

Layered Navigation

$99
61 reviews

Gift Card

$199
8 reviews

Reward Points

$99
8 reviews

Affiliate

$149
16 reviews

Shop By Brand

$99
19 reviews

People also searched for:

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