Hello, Shopify developers! I’m struggling with an issue related to the Shopify GraphQL API, specifically when trying to add a product variant to a cart. I was hoping someone here might have encountered a similar issue and could offer some guidance.
The Task:
I’m trying to create a shopping cart and then add a specific product variant to it.
The Code:
Here’s the function I’ve written to handle this:
const axios = require('axios');
const shopifyDomain = 'your-shopify-domain';
const storefrontAccessToken = 'your-access-token';
const createCartAndAddProduct = async () => {
try {
// Step 1: Create the cart
const createCartResponse = await axios.post(
`https://${shopifyDomain}/api/2023-10/graphql.json`,
{
query: `
mutation {
cartCreate(input: {}) {
cart {
id
}
userErrors {
field
message
}
}
}
`,
},
{
headers: {
'Content-Type': 'application/json',
'X-Shopify-Storefront-Access-Token': storefrontAccessToken,
},
}
);
const { cart, userErrors } = createCartResponse.data.data.cartCreate;
if (userErrors && userErrors.length > 0) {
console.error("User Errors (Cart Creation):", userErrors);
return;
}
console.log("Cart created:", cart.id);
// Step 2: Add the product to the cart
const productId = "gid://shopify/ProductVariant/44707067461897";
const addToCartResponse = await axios.post(
`https://${shopifyDomain}/api/2023-10/graphql.json`,
{
query: `
mutation AddToCart($cartId: ID!, $variantId: ID!) {
cartLinesAdd(cartId: $cartId, lines: [{ quantity: 1, merchandiseId: $variantId}]) {
cart {
lines(first: 100) {
edges {
node {
id
quantity
merchandise {
... on ProductVariant {
product {
title
}
}
}
}
}
}
}
userErrors {
field
message
}
}
}
`,
variables: {
cartId: cart.id,
variantId: productId
}
},
{
headers: {
'Content-Type': 'application/json',
'X-Shopify-Storefront-Access-Token': storefrontAccessToken,
},
}
);
const { cart: updatedCart, userErrors: addToCartErrors } = addToCartResponse.data.data.cartLinesAdd;
if (addToCartErrors && addToCartErrors.length > 0) {
console.error("User Errors (Add To Cart):", addToCartErrors);
return;
}
console.log("Product added to cart:", updatedCart.lines.edges);
} catch (error) {
console.error('Error:', error.response ? error.response.data : error.message);
}
};
createCartAndAddProduct();
The Problem:
I’ve recently added a new product to my Shopify store, and here is the response I got:
{
"product": {
"id": 8338361549065,
"title": "tester",
// Other product details
"variants": [
{
"id": 44707067461897,
"title": "Default Title",
// Other variant details
}
],
// Other product details
}
}
However, when I run the createCartAndAddProduct function, I get the following error:
User Errors (Add To Cart): [
{
"field": ["lines", "0", "merchandiseId"],
"message": "The merchandise with id gid://shopify/ProductVariant/44707067461897 does not exist."
}
]
I’ve double-checked the product variant ID, and it matches exactly with what’s in the Shopify response. I’m not sure what’s going wrong here.
The Question
Has anyone encountered a similar issue or can spot what I might be doing wrong? Any help or suggestions would be greatly appreciated!
Thank you in advance for your time and help! ![]()
full script:
const axios = require('axios');
const shopifyDomain = '4aeb95-2.myshopify.com';
const storefrontAccessToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
const createCartAndAddProduct = async () => {
try {
// Step 1: Create the cart
const createCartResponse = await axios.post(
`https://${shopifyDomain}/api/2023-10/graphql.json`,
{
query: `
mutation {
cartCreate(input: {}) {
cart {
id
}
userErrors {
field
message
}
}
}
`
},
{
headers: {
'Content-Type': 'application/json',
'X-Shopify-Storefront-Access-Token': storefrontAccessToken,
},
}
);
const { cart, userErrors } = createCartResponse.data.data.cartCreate;
if (userErrors && userErrors.length > 0) {
console.error("User Errors (Cart Creation):", userErrors);
return;
}
console.log("Cart created:", cart.id);
// Step 2: Add the product to the cart
const productId = "gid://shopify/ProductVariant/44707067461897";
const addToCartResponse = await axios.post(
`https://${shopifyDomain}/api/2023-10/graphql.json`,
{
query: `
mutation AddToCart($cartId: ID!, $variantId: ID!) {
cartLinesAdd(cartId: $cartId, lines: [{ quantity: 1, merchandiseId: $variantId}]) {
cart {
lines(first: 100) {
edges {
node {
id
quantity
merchandise {
... on ProductVariant {
product {
title
}
}
}
}
}
}
}
userErrors {
field
message
}
}
}
`,
variables: {
cartId: cart.id,
variantId: productId
}
},
{
headers: {
'Content-Type': 'application/json',
'X-Shopify-Storefront-Access-Token': storefrontAccessToken,
},
}
);
const { cart: updatedCart, userErrors: addToCartErrors } = addToCartResponse.data.data.cartLinesAdd;
if (addToCartErrors && addToCartErrors.length > 0) {
console.error("User Errors (Add To Cart):", addToCartErrors);
return;
}
console.log("Product added to cart:", updatedCart.lines.edges);
} catch (error) {
console.error('Error:', error.response ? error.response.data : error.message);
}
};
createCartAndAddProduct();