Solved

mutation createCart($cartInput: CartInput) always returns cart: null

david_hanh
Tourist
3 1 0

Following this blog article https://www.netlify.com/blog/2021/07/20/build-your-own-shop-with-the-shopify-storefront-api-eleventy...

I am trying to create a cart the first time the user add an item to the cart with the following request using the "unstable" API:

query: 

        mutation createCart($cartInput: CartInput) {
          cartCreate(input: $cartInput) {
             cart {
              id
              createdAt
              updatedAt
              lines(first:10) {
                edges {
                  node {
                    id
                    quantity
                    merchandise {
                      ... on ProductVariant {
                        id
                        title
                        priceV2 {
                          amount
                          currencyCode
                        }
                        product {
                          id
                          title
                        }
                      }
                    }
                  }
                }
              }
              estimatedCost {
                totalAmount {
                  amount
                  currencyCode
                }
                subtotalAmount {
                  amount
                  currencyCode
                }
                totalTaxAmount {
                  amount
                  currencyCode
                }
                totalDutyAmount {
                  amount
                  currencyCode
                }
              }
            }
          }
        }
      

with the following query variables:

{
  "cartInput": {
    "lines": [
      {
        "quantity": 1,
        "merchandiseId": "<SOME_MERCHANDISE_ID>"
      }
    ]
  }
}

The API always returns:

{
  "data": {
    "cartCreate": {
        "cart": null
    }
  }
}


My private app has"Read and modify checkouts" app to allow unauthenticated_write_checkouts

Thank you for your help.

 

Accepted Solution (1)
david_hanh
Tourist
3 1 0

This is an accepted solution.

Thank you. I managed to figure it out. I found this information and since I was under the lite plan, I could not add the products to the online store since I didn't have one. I thought it was enough to make the products available for my private app among the sales channels. After upgrading my plan to the basic one and after making the products available to the online store, the API eventually returned what I expected.

View solution in original post

Replies 3 (3)

david_hanh
Tourist
3 1 0

I found out that there was an option to enable the userErrors in the createCart mutation. Now the error says:
"The merchandise with id gid://shopify/ProductVariant/35090584273063 does not exist"
The product variant ID 35090584273063 exists though so I'm not sure what's happening.

danc
Visitor
1 0 0

Apologies if you're already doing this, but I think you might need to base64 encode the ProductVariant id (e.g., `btoa("gid://shopify/ProductVariant/35090584273063")`) whenever using the storefront api.

david_hanh
Tourist
3 1 0

This is an accepted solution.

Thank you. I managed to figure it out. I found this information and since I was under the lite plan, I could not add the products to the online store since I didn't have one. I thought it was enough to make the products available for my private app among the sales channels. After upgrading my plan to the basic one and after making the products available to the online store, the API eventually returned what I expected.