Magento – Magento 2: localStorage[‘mage-cache-storage’] usage

javascriptlocal-storagemagento2

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?

Best Answer

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.

Related Topic