Solved

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

Leo_Huynh
Tourist
7 1 5

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
Personal site: https://leohuynh.dev
Accepted Solution (1)

Leo_Huynh
Tourist
7 1 5

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
Personal site: https://leohuynh.dev

View solution in original post

Reply 1 (1)

Leo_Huynh
Tourist
7 1 5

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
Personal site: https://leohuynh.dev