I am trying to add a variant to the cart using ajax api.
const addToCart = async ({
variantId,
quantity,
shop,
}: {
product: number;
quantity: number;
shop: string;
}) => {
const response = await axios.post(
"https://" + shop + "/cart/add.js",
JSON.stringify({
items: [
{
id: variantId
quantity: quantity,
},
],
}),
{
headers: {
"Content-Type": "application/json",
},
},
);
};
But I get this error when the function is called
{"status":422,"message":"Cart Error","description":"Cannot find variant"}
When i try to call the addToCart function with a product variant I created from the admin page, it works fine. but when I use graphql api to create the product and variant, and add it to cart, the addToCart function did not work.