Fixing “window.checkout.quoteData” and “storeCode” Undefined Errors After Adding Items via AJAX
When working with Magento’s checkout process and attempting to update the cart item count using JavaScript after adding items via AJAX, you might encounter errors related to window.checkout.quoteData being undefined or unable to access the storeCode. This issue often arises when you’re trying to integrate custom functionality like free text ordering.
Understanding the Error
The error message appears because Magento’s default checkout process initializes certain variables (window.checkout) during the standard cart loading process. When you add items via AJAX without reloading the page, these necessary variables remain undefined, leading to the errors you’re facing:
// Sample error lines
Cannot read property 'quoteData' of undefined at quote.js:34
Cannot read property 'storeCode' of undefined at url-builder.js:12
Resolving the Issue
To fix this problem, we need to ensure that the required variables are initialized and available before trying to access them in our custom JavaScript code. Here’s a step-by-step approach:
1. Load Necessary Requires Before Initializing
First, make sure you’re loading all necessary modules before initializing your custom code. In your case, you should require both jquery and the checkout action:
require([
'jquery',
'Magento_Checkout/js/action/get-totals'
], function ($, getTotalsAction) {
// Your code here
});
2. Initialize in the Correct Context
When adding items via AJAX, ensure that your JavaScript file is loaded after the main checkout script (checkout.js). You can achieve this by setting a higher priority for your custom script.
In your module’s requirejs-config.js file, add the following configuration:
var config = {
map: {
'*': {
'customer-data': 'Magento_Customer/js/customer-data',
'mage/cookies': 'mage/cookies',
'jquery/ui': 'jquery/jquery-ui'
}
},
deps: [
'js/jquery/jquery.parsequery'
],
shim: {
'jquery/parsequery': {
deps: ['jquery']
}
}
};
3. Use Magento’s Checkout Action to Update Totals
To update the cart item count, you should use Magento’s built-in get-totals action provided by the Magento_Checkout/js/action/get-totals.js file:
require([
'jquery',
'Magento_Checkout/js/action/get-totals'
], function ($, getTotalsAction) {
'use strict';
// Update cart item count after adding items via AJAX
var deferred = $.Deferred();
getTotalsAction([], deferred);
// Handle the response and update the UI if needed
deferred.done(function (response) {
// Your code to handle the updated totals
});
});
Conclusion
To successfully update the cart item count after adding items via AJAX in Magento 2, you need to ensure that:
- All necessary modules are loaded before initializing your custom JavaScript.
- Your custom script is loaded with a higher priority than the main checkout script.
- You use Magento’s built-in
get-totalsaction to retrieve and update the cart item count.
By following these best practices, you can avoid the “window.checkout.quoteData” and “storeCode” undefined errors when working with custom AJAX functionality in your Magento 2 store.