How can I use the car add API adding a variant by id and sku?

How can I use the car add API adding a variant by id and sku?

jacobfung
Visitor
3 0 0

when i trying to fetch the cart api on page code, like this, 

jacobfung_0-1665373754267.png

but clearly there is not a right way,

jacobfung_1-1665373786830.png

 

What is the best way to finish it?

what else params should I add on this?

Replies 3 (3)

Jason
Shopify Partner
11196 226 2287

The cart AJAX api would be wanting a variant ID - not a product ID. The error makes sense since it looks like you're sending over a product ID.

That's noted in the docs:


In the following example, quantity is the amount of the variant that you want to add and id is the variant ID of that variant. You can add multiple variants to the cart by appending more objects in the items array.
https://shopify.dev/api/ajax/reference/cart#post-locale-cart-add-js

 

★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★
jacobfung
Visitor
3 0 0

Got you,

but how can I get the variant ID from my product detail page?

jacobfung
Visitor
3 0 0

how can I get the variant ID from my product detail page?