Magento 2 How To Get Data of Shopping Cart Items, Subtotal, Grand Total, Billing & Shipping Address

In Magento 2 platform, it is possible to get the data of shopping cart items, subtotal, grand total, billing & shipping address by simple code snippets which is shown in this blog.

  1. Get all the useful important information in your cart.
  2. Get the number of items in your cart with total quantity added to the cart.
  3. Get base total price and grand total price of all the items in the cart.
  4. Get the billing and shipping address.
  1. Get all the useful important 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 'Product ID: '.$item->getProductId().'<br />';
    echo 'Product Name: '.$item->getName().'<br />';
    echo 'Product Sku: '.$item->getSku().'<br />';
    echo 'Quantity: '.$item->getQty().'<br />';
    echo 'Price: '.$item->getPrice().'<br />';
    echo "<br />";            
}

2. Get the number of items in your cart with total quantity added to the cart.

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

3. Get base total price and grand total price of all the 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();

4. Get the billing and shipping address.

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

Hope this will help you guys!

Leave a Reply