How to get frontend cart data in Magento 2 using javascript

Today, we will show an easy way to get frontend cart data in Magento 2 by using javascript. 

We have to embed javascript to our frontend page. Here is an example based on our page.

Adding block to the cart page:


<?xml version="1.0"?>
<page xmlns:xsi="" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
       <referenceContainer name="content">

Creating template with our component and configuration:


<div data-bind="scope: 'custom-cart-section'" id="custom-cart-section">
   <!-- ko template: getTemplate() --><!-- /ko -->
<script type="text/x-magento-init">
       "*": {
           "Magento_Ui/js/core/app": {
               "components": {
                   "custom-cart-section": {
                       "component": "Magenest_GetFrontendCartData/js/custom-cart"

Creating a component itself, and it is the main section process to get frontend cart data:


], function ($, Component, customerData) {
   'use strict';
   return Component.extend({
       /** @inheritdoc */
       initialize: function () {

Here we use customer data as a source to get cart data. We print the cart data in the browser’s console when the cart page is loaded. This is the result of the frontend cart data:

The frontend cart data

There is some useful data such as: cart item information (product type, product sku, product id, product price, …), store id, website id, subtotal amount,… 

You can use the custom data to get cart data not only in the cart page but also everywhere in the frontend. 

You should not use requirejs to call the script directly, 

E.g: require(‘Magento_Customer/js/customer-data’).get(‘cart’)()

Because of the way the customer data script works, the above code will throw an error in several cases and it’s not recommended.

More information about customerData: 

  • It is private content that stores private data in local storage, invalidates private data using customizable rules, and synchronizes data with the backend.
  • There is a lot of data in customerData such as: cart, checkout-data, account, captcha, directory-data,…
  • The customer section was designed to cache private data in browser storage. This means that any customer section will not be updated until proper action is made. There are some exceptions when the customer section is updated such as: Store and website switching; the section_data_lifetime is expired. 

More advancedly, you can customize the data in customer data. You can add new data in cartData by plugin the function getSectionData in vendor/magento/module-checkout/CustomerData/Cart.php

Customize the data in customer data

Hope this guide is useful for you. Thank you for reading our blog and see you in the upcoming articles.


Don't want to miss out on our latest insights?
Subscribe to our newsletter.

Disclaimer: By clicking submit, you agree to share your information with us to receive news, announcements, and resources when they are available.