IDs to use when using the LineItems functions

Jousi
Shopify Partner
11 0 2

Hey there,

 
I have finally set up my entire site to work with and use data from Shopify (had my own cms before so had to rewrite a better part of my client application).
 
The one last thing that confuses me are the checkout functions (addLineItems, updateLineItems, removeLineItems).
 
Because from the docs, it seems like I need to use different ID for each of the functions, if you look at the docs examples, in the addLineItems example, it shows that I have to use variantID
when adding a new line item:
const lineItemsToAdd = [ {
    // Here they are using the variantID and not just product ID 
    variantId: 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8yOTEwNjAyMjc5Mg==', 
    quantity: 5, 
    customAttributes: [{key: "MyKey", value: "MyValue"}]
} ];
 
However, in the updateLineItems, the item object uses ID and not variantID:
const lineItemsToUpdate = [
    {
     // Here they are using regular product ID and not variantID
        id: 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0Lzc4NTc5ODkzODQ=',
        quantity: 2
    } 
];
 
And in the removeLineItems function, it only takes some ID, be it variantID or regular product ID, which I am also not sure, which is correct:
const lineItemIdsToRemove = [
// And here I guess its the regular product ID because its the same as above 
    'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0Lzc4NTc5ODkzODQ='
 ];
 
Whats even more confusing is when I for example want to remove a line item which I added via the variantID, I use its variantID again to remove it, but then I get error:
[{
  "message":"invalid id",
  "locations":[{"line":1,"column":2186}],
  "path":["checkoutLineItemsRemove"]
}]
 
And very often I am also getting:
TypeError: Cannot read property 'checkoutLineItemsAdd' of undefined
 
If anyone could clarify the use of IDs to me, I would greatly appreciate it!
 
One more thing that I was wondering about. Is there an endpoint where I could send variantID or regular product ID and get its stock state? I am doing it through iterating through products that I want to check and using the regular 
product.fetch(product.id) method. But as I dont need all of its info and variants, I wanted to see if there is something like this that I could use.
I am using it to update the cart in case a product wen out of stock.
 
Replies 3 (3)
Alex
Shopify Staff
Shopify Staff
1561 81 339

Think of line items as a resource of their own, like an instance of a variant (but not a variant). You need to provide a variant when creating a line item object so we know what variant that line item is an instance of. From there, the line item has an ID of its own, and can be referred to with that ID for when you are removing or updating line items. The only time you should need the variant ID is when creating (adding) a line item.

 

Inventory is stored in inventory levels. You could make a GraphQL query like this to get total, and location-specific inventory counts:

 

{
  productVariants(first:10){
    edges {
      node {
        inventoryQuantity
        inventoryItem {
          inventoryLevels(first:10){
            edges {
              node {
                id
		  available
                location {
                  name
                }
              }
            }
          }
        }
      }
    }
  }
}

Hope that helps a bit.

 

Cheers.

Alex | 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 the Shopify Help Center or the Shopify Blog

Jousi
Shopify Partner
11 0 2

Hey there Alex,

 

Thanks a lot, thats the information I have been missing. Nobody was able to tell me this until now, not even Shopify staff before you.

 

Cheers,

Jousi

peteczar
New Member
4 0 0

Hi,

I came across this question while also adding line items to my checkout.

Does this mean that the new id returned when calling 

client.checkout.addLineItems

needs to be stored and matched against the variantId?

Otherwise, I can't figure out how to 

client.checkout.updateLineItems

properly as I am also getting an error when trying to call updateLineItems with the id or variantId of the product.

It seems to work when I call updateLineItems with the id returned after calling addLineItems.

 

Thank you for any help.