Solved

How to get line item id in the return of checkoutLineItemsAdd mutation?

anthony-thomas
Shopify Partner
8 0 1

I want to update lineitem in the checkout, but I need to include lineitem id in the checkoutLineItemsUpdate mutation.

So I want to get lineitem id after adding a product to checkout so that I can use it when I update the lineitem. But I don't know how to get lineitem id in the return of checkoutLineItemsAdd mutation.

I can get checkout id like this.

 

mutation checkoutLineItemsAdd($lineItems: [CheckoutLineItemInput!]!, $checkoutId: ID!) {
  checkoutLineItemsAdd(lineItems: $lineItems, checkoutId: $checkoutId) {
    checkout {
      id
    }
    checkoutUserErrors {
      code
      field
      message
    }
  }
}

 

Anthony
Accepted Solution (1)

Luke_K
Shopify Staff
402 66 98

This is an accepted solution.

Hey @anthony-thomas 

Something like this should do it : 

mutation checkoutLineItemsAdd($lineItems: [CheckoutLineItemInput!]!, $checkoutId: ID!) {
  checkoutLineItemsAdd(lineItems: $lineItems, checkoutId: $checkoutId) {
    checkout {
      webUrl
      lineItems(first: 1) {
        edges {
          node {
            id
          }
        }
      }
    }

That will return you your checkoutLineItem ID for use in the checkoutLineItemsUpdate. Hope that helps!

| Shopify |
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution!

View solution in original post

Replies 8 (8)

Luke_K
Shopify Staff
402 66 98

This is an accepted solution.

Hey @anthony-thomas 

Something like this should do it : 

mutation checkoutLineItemsAdd($lineItems: [CheckoutLineItemInput!]!, $checkoutId: ID!) {
  checkoutLineItemsAdd(lineItems: $lineItems, checkoutId: $checkoutId) {
    checkout {
      webUrl
      lineItems(first: 1) {
        edges {
          node {
            id
          }
        }
      }
    }

That will return you your checkoutLineItem ID for use in the checkoutLineItemsUpdate. Hope that helps!

| Shopify |
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution!
anthony-thomas
Shopify Partner
8 0 1

@Luke_K thank you for your kind reply. This should work.

Only my concern is that I don't have the storefront access token with the access to get line items. I tried to get the new one from the private app, but the new token didn't work. It still doesn't have access. Which access scopes do I need to select?

Could you please let me know your thoughts? Am I missing anything?

Anthony
Luke_K
Shopify Staff
402 66 98

Hey @anthony-thomas 

For that call I made above to checkoutLineItemsAdd, my X-Shopify-Storefront-Access-Token specified in my private app when I make the call has the following scopes below enabled; you'd need those to be able to call checkoutLineItemsAdd, it wouldn't work without them. Let me know if that helps!

07-10-6r9vo-5uum2

| Shopify |
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution!
anthony-thomas
Shopify Partner
8 0 1

Hi @Luke_K I checked all permissions in that section, but the token does not work.

anthonythomas_0-1631031404749.png

Am I missing wrong? Could you please help me with this?

                 const mutation = {
                    query: `
                        mutation checkoutLineItemsAdd($lineItems: [CheckoutLineItemInput!]!, $checkoutId: ID!) {
                            checkoutLineItemsAdd(lineItems: $lineItems, checkoutId: $checkoutId) {
                                checkout {
                                    id
                                    lineItems(last: 5) {
                                        edges {
                                            node {
                                                id
                                                quantity
                                                variant {
                                                    id
                                                }
                                            }
                                        }
                                    }
                                }
                                checkoutUserErrors {
                                    code
                                    field
                                    message
                                }
                            }
                        }
                    `,
                    variables: {
                        lineItems: [
                            {
                                quantity: quantity,
                                variantId: variantId,
                                customAttributes,
                            },
                        ],
                        checkoutId: checkoutId,
                    },
                };

                const settings = {
                    async: true,
                    crossDomain: true,
                    url: '***',
                    method: 'POST',
                    headers: {
                        'X-Shopify-Storefront-Access-Token': '***',
                        'Content-Type': 'application/json',
                    },
                    data: JSON.stringify(mutation),
                };

                $.ajax(settings).always(result => {
                    console.log(result);
                });

 

Anthony
Luke_K
Shopify Staff
402 66 98

Hey @anthony-thomas 

I'm thinking if the X-Shopify-Storefront-Access-Token is correct, it has to be something passed in the settings const or the headers.

So one thing I noticed in your settings const, the Content-Type header is application.json.

I would try changing that to 'application/graphql'. I figured you were just omitting the url in the settings const too, but ensure it's going to the Storefront API endpoint and not the admin endpoint (that catches people out sometimes).

 

| Shopify |
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution!
anthony-thomas
Shopify Partner
8 0 1

@Luke_K 

Is there any way to get the line item id from the checkout object in the liquid file? Actually, I got one using this link

https://shopify.dev/api/liquid/objects/line_item

And I tried to encode it to base 64 code. 

gid://shopify/LineItem/******** -> base64 code

But it is saying invalid id. Do you have any idea about this?

Anthony
Luke_K
Shopify Staff
402 66 98

Hey there,

Alas, Liquid is technically out of scope for my team, but to the best of my knowledge I don't believe this approach is possible via Liquid at this time. You may want to check in the Liquid forums though. Thanks!

 

| Shopify |
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution!
pawlka
Visitor
2 0 4

Hi @Luke_K,

 

The line items returned by the Storefront Checkout and Cart API do not seem to be sorted in any predictable fashion, meaning that your query will not return the line item which was last added to cart but merely any one of the line items.

I kindly request your comment on this thread https://community.shopify.com/c/shopify-apis-and-sdks/storefront-api-cart-checkout-line-items-order-...