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
2 Answers 2
Try below code:
define(['jquery', 'jquery/ui', 'Magento_Customer/js/customer-data'], 
function(,ドルitem,customerData){
 var summaryCount = customerData.get('cart')().summary_count; 
 var item = customerData.get('cart')().items;
 console.log(summary_count);
 console.log(item.length);
});
- 
 it gives define error, as i have to used in phtml file,instead of define i changed to require, but same issue..sayd summary_count undefineRahul Singh– Rahul Singh2020年02月03日 02:57:32 +00:00Commented Feb 3, 2020 at 2:57
- 
 Uncaught TypeError: Cannot read property 'length' of undefined Uncaught TypeError: Cannot read property 'summary_count' of undefinedRahul Singh– Rahul Singh2020年02月03日 03:07:55 +00:00Commented Feb 3, 2020 at 3:07
you have to add a sequence into your module.xml
etc/module.xml
<?xml version="1.0"?>
<config>
 <module name="Module_Name">
 <sequence>
 <module name="Magento_Customer"/>
 </sequence>
 </module>
</config>
- 
 It's not right. Every JS module in the system can be loaded on demand, sosequencedoesn't matter for frontend.Evgeny Levinsky– Evgeny Levinsky2020年04月03日 16:39:20 +00:00Commented Apr 3, 2020 at 16:39
- 
 Welcome to stackexchange!Evgeny Levinsky– Evgeny Levinsky2020年04月03日 16:40:11 +00:00Commented Apr 3, 2020 at 16:40