I am trying to get the cart items in phtml file (product detail page) using jquery script.
Here is the script:
require([ 'jquery', 'jquery/ui'], function($,item){
item = require('Magento_Customer/js/customer-data').get('cart')().items;
console.log(item.length);
});
It did not load customer data all the time. I need this to get the item count when a product is added or deleted from the mini cart dynamically using ajax cart. On firefox, its works but fails in chrome browser. I don't know how to initialize the requirement ('Magento_Customer/js/customer data) early so that it works well for all cases.
Please help or provide other solutions to get the cart item dynamically using jquery
Best Answer
Try below code: