Update cart attributes with AJAX /update.js got PHP string saved in cart object

Solved

Hi

I'm trying to update cart attributes like this

fetch('/cart/update.js', {
    method: "POST",
    headers: {
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/json;'
    },
    body: JSON.stringify({
        attributes: {
            foo: { bar: "123" }
        }
    })
})

 

It successfully updates the cart, saves the result as a string (it should be an object, right?)

But... the string is a stringified object of PHP, not Javascript

Leo_Huynh_0-1627554776789.png

Does anyone know how to deal with this?

 

Leo Huynh | Shopify Software Engineer @ Insights Studio
Email: leo@insights.is
0 Likes

This is an accepted solution.

So I found my temporary solution

- stringify the foo object
- send it
- parse back when need to use

But I guess that it shouldn't be the official way!

Leo Huynh | Shopify Software Engineer @ Insights Studio
Email: leo@insights.is
0 Likes