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

Magento 2 is popular owing to its capability of customization and flexibility. A store owner can twist the default features to improve the customer experience.

Similarly, there was a business requirement where the client wanted to improve the shopping cart features for which I had to get the data of shopping cart items, subtotal, grand total, billing & shipping address in Magento 2.

Therefore, I’ve posted this solution here which you can bookmark to use whenever you need to work on shopping cart and need to retrieve these data.

For example, you may want to restrict the number of shopping cart items, or limit the total amount or allow particular shipping address only. Such scenarios require you to get shopping cart data in Magento 2 for which you can refer the below code.

Method to get the data of shopping cart items, subtotal, grand total, billing & shipping address in Magento 2:

Create ShippingDetail.php file at Vendor\Extention\Model\Carrier Folder

<?php
namespace Vendor\Extention\Model\Carrier;
use Magento\Checkout\Model\Cart;
use Magento\Framework\View\Element\Template;
use Magento\Backend\Block\Template\Context;
class Shippingperitem extends Template
{
    protected $cart;
    public function __construct(Context $context, Cart $cart, array $data = [])
    {
        $this->cart = $cart;
        parent::__construct($context, $data);
    }
    public function getItems()
    {
        $itemsCollection = $this->cart->getQuote()->getItemsCollection();
        // get quote items array
        $items = $this->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 />';
        }
    }
    //Get number of items and total quantity from cart.
    public function getNumberOfItems()
    {
        $totalItems = $this->cart->getQuote()->getItemsCount();
        $totalQuantity = $this->cart->getQuote()->getItemsQty();
        echo 'Items count :' . $totalItems . '<br />';
        echo 'Items qty :' . $totalQuantity . '<br />';
    }
    //Get base total price and grand total price of items from cart.
    public function getPrice()
    {
        $subTotal = $this->cart->getQuote()->getSubtotal();
        $grandTotal = $this->cart->getQuote()->getGrandTotal();
        echo 'Sub Total :' . $subTotal . '<br />';
        echo 'Grand Total :' . $grandTotal . '<br />';
    }
    //Get selected Shipping and Billing address from cart.
    public function getAddress()
    {
        $billingAddress = $this->cart->getQuote()->getBillingAddress();
        $shippingAddress = $this->cart->getQuote()->getShippingAddress();
        echo '<pre>';
        print_r($billingAddress->getData());
        echo '</pre>';
        echo '<pre>';
        print_r($shippingAddress->getData());
        echo '</pre>';
    }
}

That’s it.

Do share the solution with fellow Magento developers via social media and make it easy for them to customize the shopping cart!

Thank you.

Sanjay Jethva

Article by

Sanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for...