Ajax API - cart/change.js Endpoint - Wrong Documentation ?

lukaseberlein
New Member
3 0 0

Hi,

 

could someone please check the Documentation about the "cart/change.js" Endpoint --> https://shopify.dev/docs/themes/ajax-api/reference/cart ? Or let me know if i am wrong. 

In my opinion there is an error in here. I already read a lot of q&a of people having problems with changing line items. So had i. After testing out parameters i realised that i had to pass the ID Value as a String. 

let data = { "id" : "213872167216""quantity" : }  ---> works

let data = { "id" : 213872167216"quantity" : }  ---> does not work - but is the schema of the doc

 

It would be great if a Shopify Expert could reply on this one. Either correct me Or correct the Doc. 

 

 

Thanks a lot!

Best

Lukas

 

 

0 Likes
gina-gregory
Shopify Expert
710 48 176

Without quotes around the ID does work, or can work, I should say. It may be an issue with your ajax call. Can you post your code?

lukaseberlein
New Member
3 0 0

Hi,

 

export const updateItem = async (variantID, quantity) => {

    let data = {
        "id" : variantID,
        "quantity" : quantity
    }

    let cartContents = fetch('/cart/change.js', {
        method: 'POST',
        credentials: 'same-origin',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(data)
    })
    .then(response => {
        return response.json();
    })
    .catch((error) => {
        console.error('Error:', error);
    })

    return cartContents
}

 

 

So this works for me but only if i pass the "variantID" as a String by: let variantID = id.toString()

Thanks!

Best.

Lukas

 

 

0 Likes
gina-gregory
Shopify Expert
710 48 176

Ok, this seems to be more of a requirement for using fetch, rather than an issue on Shopify's end. https://github.github.io/fetch/#request-body

Screen Shot 2021-01-07 at 9.00.12 AM.png

0 Likes
lukaseberlein
New Member
3 0 0

Hello,

 

thanks for th answer. But this does not make sense to me. First of all: Why can i pass the quantity as a "number" ?

Secondly: For adding an item i use the exact same Pattern:

export const addItem = async (variantID, quantity) => {

    let formData = {
        'items': [{
            'id': variantID,
            'quantity': 1
        }]
    }

    let cartContents = fetch('/cart/add.js', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(formData)
    })
    .then(response => {
        return response.json();
    })
    .catch((error) => {
        console.error('Error:', error);
    })

    return cartContents
}

 

But here i can pass the variantID as a "number". I really tested this multiple times. I read many forum posts of people having trouble with this. At the end all works as expected BUT i need to pass the variant ID as a String ONLY at  /cart/change.js

 

If you have any more questions please let me know.

0 Likes