Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

cartCreate only creates an empty cart

Solved

cartCreate only creates an empty cart

AlanForsVispero
Tourist
6 1 0

I'm using version 2021-10 (release candidate) of the api to create a shopping cart. The cart is created, but it's always empty. If I intentionally set the merchandiseId to an invalid value, it correctly reports a user error for an invalid merchandiseId. When I use a valid merchandiseId, the cart is created but contains an empty lines array.

My query:

 

mutation ($id: ID!, $auth: String!, $email: String!) {
  cartCreate(input: {
    lines: [
      {
        merchandiseId: $id, 
        quantity: 1, 
        attributes: [
          {
            key: "AuthenticationCode", 
            value: $auth
          }
        ]
      }
    ], 
    buyerIdentity: {
      email: $email
    }
  }) 
  {
    cart {
      id
      checkoutUrl
      lines(first: 5) {
        edges {
          node {
            id
            merchandise {
              ... on ProductVariant {
                id
              }
            }
          }
        }
      }
    }
    userErrors {
      code
      field
      message
    }
  }
}

 

And the query variables:

 

{
  "id":"<variantId>",
  "auth":"F54596DE5C661DBDB8A4",
  "email":"afors@vispero.com"
}

 

 

I get the response:

 

{
  "data": {
    "cartCreate": {
      "cart": {
        "id": "Z2lkOi8vc2hvcGlmeS9DYXJ0L2RiNmJiNGFhZjYyZDg1YjMxZTJkYmY2YTgxZTY4MDJj",
        "checkoutUrl": "https:\/\/fs-recurring-billing-sandbox.myshopify.com\/cart\/c\/db6bb4aaf62d85b31e2dbf6a81e6802c",
        "lines": {
          "edges": []
        }
      },
      "userErrors": []
    }
  }
}

 

Can anyone see a problem with this? I've tried everything I can think of.

 

 

 

Accepted Solution (1)

AlanForsVispero
Tourist
6 1 0

This is an accepted solution.

The item I was adding to the cart was a subscription-only variant. As such, it was required to include a sellingPlanId with the line item as below.

  cartCreate(input: {
    lines: [
      {
        merchandiseId: $id, 
        quantity: 1, 
        attributes: [
          {
            key: "AuthenticationCode", 
            value: $auth
          }
        ],
        sellingPlanId: $sellingPlanId
      }
    ], 
    buyerIdentity: {
      email: $email
    }
  }) 

All possible sellingPlans can be found with a product query:

{
  products(first: 4, query: "title:'My subscription Product'") {
    edges {
      node {
        title
        handle
        id
        variants(first: 5) {
          edges {
            node {
              title
              id
              priceV2 {
                amount
              }
              sku
              sellingPlanAllocations(first: 5) {
                edges {
                  node {
                    sellingPlan {
                      id
                      name
                      options {
                        name
                        value
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    pageInfo {
      hasNextPage
    }
  }
}

View solution in original post

Replies 2 (2)

AlanForsVispero
Tourist
6 1 0

I decided to check for an empty cart after creation, and if it is empty, then use a cartLinesAdd mutation to add the line item. Same result. The query succeeds, but the cart is still empty.

AlanForsVispero
Tourist
6 1 0

This is an accepted solution.

The item I was adding to the cart was a subscription-only variant. As such, it was required to include a sellingPlanId with the line item as below.

  cartCreate(input: {
    lines: [
      {
        merchandiseId: $id, 
        quantity: 1, 
        attributes: [
          {
            key: "AuthenticationCode", 
            value: $auth
          }
        ],
        sellingPlanId: $sellingPlanId
      }
    ], 
    buyerIdentity: {
      email: $email
    }
  }) 

All possible sellingPlans can be found with a product query:

{
  products(first: 4, query: "title:'My subscription Product'") {
    edges {
      node {
        title
        handle
        id
        variants(first: 5) {
          edges {
            node {
              title
              id
              priceV2 {
                amount
              }
              sku
              sellingPlanAllocations(first: 5) {
                edges {
                  node {
                    sellingPlan {
                      id
                      name
                      options {
                        name
                        value
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    pageInfo {
      hasNextPage
    }
  }
}