4

I've been looking at an easy and fast way (not expensive) to get the current cart data on each page view.

It seems that most information about the cart is available in LocalStorage. You can see what's there by running this:

JSON.parse(localStorage['mage-cache-storage']);

Does anyone know when this is available? And if it is safe to use and assume it will be there?

7

You can check this answer out:

Magento 2: Syncing Backend and Frontend State/Cache

Here's an example how to use customer-data:

define([
    "Magento_Customer/js/customer-data"
], function (customerData) {
    // Get data
    var customData = customerData.get("custom-data")();

    // Change something
    customData.foo = "bar";

    // Set data
    customerData.set("custom-data", customData);
});

Hope this'll help you out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.