How to add product in cart using Node and graphql apis (storefront api)?

sajid1122
Shopify Partner
1 0 0

Hi,

I'm trying to add line item to an cart. I'm using this mutation query

mutation addCartLines($cartId: ID!, $lines: [CartLineInput!]!) {
  cartLinesAdd(cartId: $cartId, lines: $lines) {
    cart {
    id
        lines(first: 10){
            edges
            {
                node{
                    quantity
                    merchandise{
                        ... on ProductVariant {   						
                            id
                        }
                    }
                }
            }
        }
        cost {
        totalAmount {
          amount
          currencyCode
        }
        subtotalAmount {
          amount
          currencyCode
        }
        totalTaxAmount {
          amount
          currencyCode
        }
        totalDutyAmount {
          amount
          currencyCode
        }
      }   
}


    userErrors {
      field
      message
    }
  }
}

and the variables are 

{
	"cartId": "gid://shopify/Cart/e623277ec9e65c98f583268f06900ce7",
	"lines": {
		"merchandiseId": "gid://shopify/ProductVariant/40993523892280",
		"quantity": 3
	}
}

but the response is

{
  "data": {
    "cartLinesAdd": {
      "cart": {
        "id": "gid://shopify/Cart/c1-a62feac56c657187bfb475d7b6695976",
        "lines": {
          "edges": []
        },
        "cost": {
          "totalAmount": {
            "amount": "0.0",
            "currencyCode": "USD"
          },
          "subtotalAmount": {
            "amount": "0.0",
            "currencyCode": "USD"
          },
          "totalTaxAmount": null,
          "totalDutyAmount": null
        }
      },
      "userErrors": [
        {
          "field": [
            "cartId"
          ],
          "message": "The specified cart does not exist."
        },
        {
          "field": [
            "lines",
            "0",
            "merchandiseId"
          ],
          "message": "The merchandise with id gid://shopify/ProductVariant/40786030821473 does not exist."
        }
      ]
    }
  }
}

however the cart id and the varient id is correct. Can't figure out what I'm doing wrong?

Replies 3 (3)

Vinod_GWL
Shopify Partner
2 0 0

Have you got any solution for this?Right now i am facing the same issue at my end

Rehmatsayany
Shopify Partner
1 0 0

Mutation

mutation AddProductToCart($cartId: ID!, $lines: [CartLineInput!]!) {
cartLinesAdd(cartId: $cartId, lines: $lines) {
cart {
id
createdAt
updatedAt
attributes {
key
value
}
lines(first: 10) {
edges {
node {
id
quantity
merchandise {
... on ProductVariant {
id
}
}
}
}
}
}
}
}

variable

{
"cartId": "gid://shopify/Cart/Z2NwLXVzLWVhc3QxOjAxSERINVFKN1JFTUFaMFc1NEtSMzdFQ0tX",
"lines": {
"quantity" : 5,
"merchandiseId": "gid://shopify/ProductVariant/45321787015485"

}
}
umang-pancholi
Shopify Partner
20 0 0