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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?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.
Any doubts about the solution or its implementation? Please mention them in the Comments section below. I’d be happy to help.
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
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 businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
Get The Easiest Way To Call Phtml File In CMS Static Block In Magento 2
Learn the One Simple Method To Insert Record In Database In Magento
Next