Magento 2: Some Useful Scripts to Check customerData

Here are some useful scripts to check the status of items of customerData and force to reload the customer Data in Section

Posted on June 7, 2021 in JavaScript, Magento2

1) Check the items inside customer wishlist section.


require(['Magento_Customer/js/customer-data'], function(customerData){
    
    var wishlist = customerData.get('wishlist')();
    //console.log('debug', wishlist);
    if(wishlist.items) {
        var wishlistItems = wishlist.items;

        for (var i = 0; i < wishlistItems.length; i++) {
            var product = wishlistItems[i];
            console.log(product.product_name);
            console.log(product.image);
        }
    }
});

2) Check ‘mage-cache-storage’ cookie to get all customerData value

CustomerData works with the data stored on the client side. Magento 2 sections are merged parts of customer data. They allow keeping this data up-to-date via a sync with the web server. Every section is a key in magento-cache-storage, while magento-cache-storage is the key in localStorage.

Or you can just view the CustomerData with Url: {store_url}/customer/section/load/?sections=[custom_section_name]. Replace [customer_section_name] with real name, wishlist, cart…

Just copy&paste the following statement into web browser console.


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

3) Force to reload the some customer sections, for example ‘wishlist’


<script>
require([
   'Magento_Customer/js/customer-data'
], function (customerData) {
   "use strict";
   var sections = ['wishlist'];
   customerData.invalidate(sections);
   customerData.reload(sections, true);
});
</script>

4) Find all xml files with ‘cacheable=”false”’

If this attribute found inside default.xml, then the whole website is not cacheable.


grep -rnw 'app/' -e 'cacheable="false"'


comments powered by Disqus