Im using the Shopify storefront API to query for a list of products and add a selected item to the cart.
I am able to use API to list all the products, and returns the variantID for the product found
Here is the GraphQL query to return a product
{
shop {
name
products(first: 1, query:"title=configurable-handmade-concrete-ball") {
edges {
cursor
node {
id
title
handle
variants(first:1) {
edges {
node {
id
title
}
}
}
}
}
}
}
}
and the result
{
"data": {
"shop": {
"name": "VonageTest",
"products": {
"edges": [
{
"cursor": "eyJvZmZzZXQiOjF9",
"node": {
"id": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzEwNTU2MjYxNTE4",
"title": "Configurable Handmade Concrete Ball",
"handle": "configurable-handmade-concrete-ball",
"variants": {
"edges": [
{
"node": {
"id": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC80MDIwOTc1NjQzMA==",
"title": "Default Title"
}
}
]
}
}
}
]
}
}
}
}
In order to add items to the cart, you can make a POST request that contains the following
https://{store_name}.myshopify.com/cart/{variant_id}
performing this call using the variant_id from the graphQL response returns a 404. But if you get the variant_id from the page, you can inspect the xml page and use the variant_id there
This shows how that is done
https://help.shopify.com/themes/customization/cart/use-permalinks-to-preload-cart
so why does the variant_id from the storefront API different from the variant_id on the page?
3
Answers
I just encountered the same problem and was finally able to track down the answer in the Shopify GraphQL docs – https://help.shopify.com/api/storefront-api/reference/scalar/id
Basically, the
id
returned in Shopify GraphQL responses is a base64-encoded representation of the actual Shopifyid
. So if you base64-decode the variantid
from the result you posted, the value isgid://shopify/ProductVariant/40209756430
You would need to parse the numerical
id
from the end of that value and that would be theid
that Shopify uses for all other APIs.I have implemented the shopify shop and cart using GraphQL provided by shopify . Follow these steps –
Storefront.CheckoutLineItemInput(quantity,new ID(productVariantId));
Don’t be confused with productId with productVariantID . You need to use productVariantId hereNow you need to mutate line items on CheckoutCreateInput object –
You will get a checkout Id here , save it .
4.Now you need to create Storefront.MailingAddressInput() , taking inputs from user (city , state , email , name etc).Then you need to update this mailing address on CheckoutCreateInput() object like this checkoutCreateInputObj.setShippingAddress() .
5.Now you need to fetch Shipping Rates –
Fetch the total price user need to pay –
Apply coupons if any –
Get the cardVaultURL –
Get the payment token –
cardClient.vault(creditCard, cardVaultURL).enqueue(new CreditCardVaultCall.Callback() {
@Override public void onResponse(@NonNull String token) {
// proceed to complete checkout with token
paymentToken = token;
}
Charge amount –
I have two utility functions that I’ve been using to flip back and forth between the
gid
and restid
:like previous posts mentioned your response is a base64 encoded version of the
gid://shopify/ProductVariant/${id}
so you’d have to add that decoding/encoding into these, I think the GraphQL library I’m using automatically decodes ids.