Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

cartLinesUpdate returns error invalid id

cartLinesUpdate returns error invalid id

anxiouspenguin
Shopify Partner
10 2 1

Hi,

I am using vanilla JavaScript with GraphQL to update a line-item quantity on the Cart page. The details are below.

 

Code:

const endpoint = 'https://helloworld.myshopify.com/api/2024-07/graphql.json';
const headers = {
  'Content-Type': 'application/json',
  'X-Shopify-Storefront-Access-Token': ''
};
const mutation_query = `mutation updateCartLines($cartId: ID!, $lines: [CartLineUpdateInput!]!) {
  cartLinesUpdate(cartId: $cartId, lines: $lines) {
    userErrors {
      code
      field
      message
    }
    cart {
      id
      lines(first: 5) {
        edges {
          node {
            id
            quantity
            attributes {
              key
              value
            }
          }
        }
      }
    }
  }
}`;
const options = {
  method: 'POST',
  headers,
  body: JSON.stringify({
    query: mutation_query,
    variables: {
      cartId: `gid://shopify/Cart/${cart_token}`,
      lines: [
        {
          id: `gid://shopify/CartLine/${line_id}`,
          quantity: 2
        }
      ]
    }
  })
};

// Fetch
fetch(endpoint, options)
.then(response => {
  console.log('response:', response);

  return response.json();
})
.then(data => {
  console.log('data:', data)
})
.catch(error => {
  console.error(error);
});

Response:

{
    ok: true,
    status: 200
}

Data with errors:

{
    "data": {
        "cartLinesUpdate": null
    },
    "errors": [
        {
            "message": "invalid id",
            "path": [
                "cartLinesUpdate"
            ],
            "locations": []
        }
    ]
}

 

All help is appreciated! I have been stuck on this for hours and have tried everything I can think of and searched everything I can think of.

 

Thank you,

Michael

Replies 7 (7)

Liam
Community Manager
3108 344 902

Hi Michael,

 

Are you sure these variables are correct:

 

variables: {
      cartId: `gid://shopify/Cart/${cart_token}`,
      lines: [
        {
          id: `gid://shopify/CartLine/${line_id}`,
          quantity: 2
        }

 

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

anxiouspenguin
Shopify Partner
10 2 1

Hey Liam,

 

I think they are but let me know if this looks correct to you.

 

cartID:

gid://shopify/Cart/Z2NwLXVzLWNlbnRyYWwxOjAxSjJNQ1JZMzJYMUtHTlNUQlFCQlFGNkVC?key=006d49fba596076f41c25a394571cd26

 

This came from the cart.js API. The variable is const cart_token = cart.token;

 

id attempt #1:

gid://shopify/CartLine/1
 
This came from incrementing in the line-items foreach loop.
 
id attempt #2:
gid://shopify/CartLine/47572753711389

 

This came from the cart.js API. I noticed the ID is the same as the variant ID. The variables are const line_items = cart.items; and const line_id = line_item.id;

 

Thank you,

Michael

Eric-HAN
Shopify Partner
275 30 29

HI , there

 It seems that the format "attempt#1" and "attempt#2" you mentioned follows the format of merchandise fields, rather than the cart line ID format.

see pic below

EricHAN_0-1721620967807.png

 

You d better use query cart Graphql to check the ids

query {
  cart(id: "gid://shopify/Cart/xxxxxxxxx") {
    # Cart fields
    id
    checkoutUrl
    createdAt
    lines(first:5){
        nodes{
            id
            quantity
            merchandise{
                ... on ProductVariant{
                    id
                }
            }
        }
    }
  }
}
- Helpful? Please hit Like and mark it as a solution
Want to modify or custom changes on store? Let me help.
- Feel free to Email Me    Buy Me A Coffee
Tobi-van
Visitor
1 0 0

Hi there,

 

@anxiouspenguin, I am having the exact same use case as you, tried all the things you tried and am running into the same problem. Funny your post is only 3 days old!

Have you been able to fix it already? The problem still persists for me unfortunately.

 

I'm assuming you also used these docs here: https://shopify.dev/docs/storefronts/headless/building-with-the-storefront-api/cart/manage#step-3-in...

 

There it's clearly stated to do it this way, and if something isn't correct about this way of doing it, then Shopify should urgently update their documentation!

 

Regards,

Silvan

anxiouspenguin
Shopify Partner
10 2 1

Hey @Tobi-van,

 

Unfortunately, I'm still stumped. If I ever figure this out I will reply and let you know! Please let me know if you figure it out!

anxiouspenguin
Shopify Partner
10 2 1

Hey @Eric-HAN,

 

What is the CartLine Global ID format? Where and how did you get your CartLine Global ID? As @Tobi-van mentioned, there is no Line ID like this mentioned in any documentation or even on the internet that I can find. The Line ID is always mentioned as Variant ID or a Line Key but neither of those work and neither of those are formatted like your Line ID. Your help is much appreciated!

 

Thank you,

Michael

Eric-HAN
Shopify Partner
275 30 29

could query your ids first. what is your result? could you give the screenshot?

query {
  cart(id: "gid://shopify/Cart/xxxxxxxxx") {
    # Cart fields
    id
    checkoutUrl
    createdAt
    lines(first:5){
        nodes{
            id
            quantity
            merchandise{
                ... on ProductVariant{
                    id
                }
            }
        }
    }
  }
}

 

- Helpful? Please hit Like and mark it as a solution
Want to modify or custom changes on store? Let me help.
- Feel free to Email Me    Buy Me A Coffee