Currently, I am utilizing the GET/cart.js
call in an asset on my theme to retrieve the cart contents in a JSON object format as shown below:
var response = jQuery.getJSON('/cart.js');
Afterward, I attempt to display the contents of the object in the console like this (within the same snippet file, immediately after making the call):
$( document ).ready(function() {
console.log(response.responseJSON.items[0]);
});
However, when I do so, I encounter a
TypeError: response.responseJSON is undefined
error in the console. Strangely enough, if I try to log the same thing again within the console (console.log(response.responseJSON.items[0]);
), it works perfectly and prints out the first item from the cart.
I've experimented with checking the object until it's no longer undefined using a while
loop, and also attempted logging only after the page has fully loaded.
Just for clarity, here are the complete contents of my snippet:
var response = jQuery.getJSON('/cart.js');
$( document ).ready(function() {
console.log(response.responseJSON.items[0]);
});
Any suggestions on how I can make this function directly within my snippet file?
EDIT: It's worth mentioning that the object is already parsed, so that isn't why I'm receiving an undefined message. I did try parsing it again just to see, but encountered another error.
EDIT 2: Here is the JSON data retrieved from the call:
{
"readyState": 4,
"responseText": "{\"token\":\"4472c3d68931e8fe2bff0afcca67a188\",\"note\":null,\"attributes\":{},\"original_total_price\":39800,\"total_price\":39800,\"total_discount\":0,\"total_weight\":0.0,\"item_count\":2,\"items\":[{\"id\":16640068878400,\"properties\":null,\"quantity\":2,\"variant_id\":16640068878400,\"key\":\"16640068878400:94cf8752e20f28a3f675ee10f8e5cc72\",\"title\":\"Compleu Abby - 60\",\"price\":19900,\"original_price\":19900,\"discounted_price\":19900,\"line_price\":398...