Where is the documentation on how to add items to cart with GraphQL's mutation or with REST?

Solved
MayneWeb
Tourist
14 0 1

Iv'e searched high and low on answers how to use add items to Cart. I'm building a headless storefront (challenging, I know), and I need to understand how the items are being added to Cart.

 

I've look here and see no mentioning of "Cart". My backend for this setup is using the Ruby gem.

 

Based on how I see carts work,  a customer, authenticated or not, can add something to cart, they can refresh their browser and it's still there so something unique is happening and I see no documentation on how to "add to cart".

Posts/replies by @MirMayne
Accepted Solution (1)
Best-Commerce
Shopify Partner
40 8 5

This is an accepted solution.

Hi @MayneWeb 

I don't know if this is the exact solution. I'm guessing Shopify manages Cart by storing the information about products in browser cookies and then creates checkout at the backend once the user proceeds for checkout. You can do similarily, here is the doc for storefront checkout API https://help.shopify.com/en/api/storefront-api/guides/checkout-guide#creating-the-checkout 

View solution in original post

Replies 3 (3)
Best-Commerce
Shopify Partner
40 8 5

This is an accepted solution.

Hi @MayneWeb 

I don't know if this is the exact solution. I'm guessing Shopify manages Cart by storing the information about products in browser cookies and then creates checkout at the backend once the user proceeds for checkout. You can do similarily, here is the doc for storefront checkout API https://help.shopify.com/en/api/storefront-api/guides/checkout-guide#creating-the-checkout 

Benjaminlotan
New Member
2 0 0

So it seems like you are suggesting that a headless store should maintain and manage their own cart, and only rely on shopify when the user is going to "checkout" and pay... am I clear on that? 

it seems @MayneWeb is trying to implement using the shopify native cart. (only product pages are headless, and add to cart ideally sends them to the shopify hosted cart). 

akiraw
Shopify Partner
5 0 0

This is old but I don't see that the other answers point to a specific solution.

Shopify does manage all the contents of a cart on the backend inside their system.

To add an item to the cart you are doing a mutation to the checkout, this specific API call 

checkoutLineItemsAdd here: https://shopify.dev/docs/api/storefront/2023-04/mutations/checkoutLineItemsAdd

 

I'm using something like this example in javascript here: https://github.com/Shopify/js-buy-sdk/blob/master/src/graphql/checkoutLineItemsAddMutation.graphql