{"id":912,"date":"2020-03-29T20:17:29","date_gmt":"2020-03-29T20:17:29","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/03\/29\/get-data-of-shopping-cart-items-subtotal-grand-total-billing-shipping-address-in-magento-2\/"},"modified":"2025-05-22T14:39:06","modified_gmt":"2025-05-22T09:09:06","slug":"get-data-of-shopping-cart-items-subtotal-grand-total-billing-shipping-address-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-data-of-shopping-cart-items-subtotal-grand-total-billing-shipping-address-in-magento-2\/","title":{"rendered":"How To Get The Data Of Shopping Cart Items, Subtotal, Grand Total, Billing &#038; Shipping Address In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Similarly, there was a business requirement where the client wanted to improve the shopping cart features for which I had to&nbsp;<em><strong>get the data of shopping cart items, subtotal, grand total, billing &amp; shipping address in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Therefore, I\u2019ve posted this solution here which you can bookmark to use whenever you need to work on shopping cart and need to retrieve these data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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&nbsp;<em>get shopping cart data in Magento 2<\/em>&nbsp;for which you can refer the below code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to get the data of shopping cart items, subtotal, grand total, billing &amp; shipping address in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><strong>ShippingDetail.php<\/strong><\/em>&nbsp;file at&nbsp;<strong>Vendor\\Extention\\Model\\Carrier<\/strong>&nbsp;Folder<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Extention\\Model\\Carrier;\nuse Magento\\Checkout\\Model\\Cart;\nuse Magento\\Framework\\View\\Element\\Template;\nuse Magento\\Backend\\Block\\Template\\Context;\nclass Shippingperitem extends Template\n{\n    protected $cart;\n    public function __construct(Context $context, Cart $cart, array $data = [])\n    {\n        $this->cart = $cart;\n        parent::__construct($context, $data);\n    }\n    public function getItems()\n    {\n        $itemsCollection = $this->cart->getQuote()->getItemsCollection();\n        \/\/ get quote items array\n        $items = $this->cart->getQuote()->getAllItems();\n        foreach ($items as $item)\n        {\n            echo 'ID: ' . $item->getProductId() . '&lt;br \/>';\n            echo 'Name: ' . $item->getName() . '&lt;br \/>';\n            echo 'Sku: ' . $item->getSku() . '&lt;br \/>';\n            echo 'Quantity: ' . $item->getQty() . '&lt;br \/>';\n            echo 'Price: ' . $item->getPrice() . '&lt;br \/>';\n        }\n    }\n    \/\/Get number of items and total quantity from cart.\n    public function getNumberOfItems()\n    {\n        $totalItems = $this->cart->getQuote()->getItemsCount();\n        $totalQuantity = $this->cart->getQuote()->getItemsQty();\n        echo 'Items count :' . $totalItems . '&lt;br \/>';\n        echo 'Items qty :' . $totalQuantity . '&lt;br \/>';\n    }\n    \/\/Get base total price and grand total price of items from cart.\n    public function getPrice()\n    {\n        $subTotal = $this->cart->getQuote()->getSubtotal();\n        $grandTotal = $this->cart->getQuote()->getGrandTotal();\n        echo 'Sub Total :' . $subTotal . '&lt;br \/>';\n        echo 'Grand Total :' . $grandTotal . '&lt;br \/>';\n    }\n    \/\/Get selected Shipping and Billing address from cart.\n    public function getAddress()\n    {\n        $billingAddress = $this->cart->getQuote()->getBillingAddress();\n        $shippingAddress = $this->cart->getQuote()->getShippingAddress();\n        echo '&lt;pre>';\n        print_r($billingAddress->getData());\n        echo '&lt;\/pre>';\n        echo '&lt;pre>';\n        print_r($shippingAddress->getData());\n        echo '&lt;\/pre>';\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do share the solution with fellow Magento developers via social media and make it easy for them to customize the shopping cart!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;.<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-912","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/912","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=912"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/912\/revisions"}],"predecessor-version":[{"id":15000,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/912\/revisions\/15000"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}