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

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

Does anyone know how to deal with this?

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!

1 Like