sveltekit-commerce icon indicating copy to clipboard operation
sveltekit-commerce copied to clipboard

The addToCart mutation now uses merchandiseId instead of variantId

Open endigo9740 opened this issue 2 years ago • 0 comments

First off I appreciate that you've shared this template. It's helped me greatly in setting up a Sveltekit-powered Shopify storefront. That said, it could probably stand for an update soon. The API is still defaulting to /2021-10/ version of the API from October 2021.

Most of the API I've touched has remained the same, but I've been roadblocked the last few days because one key name has changed...

In shopify.js the addToCart mutation key variantId is now merchandiseId.

Here's the full modification to the function:

export async function addToCart({ cartId, merchandiseId }) {
    return shopifyFetch({
        query: `
      mutation addToCart($cartId: ID!, $lines: [CartLineInput!]!) {
        cartLinesAdd(cartId: $cartId, lines: $lines) {
          cart {
            lines(first: 100) {
              edges {
                node {
                  id
                  quantity
                  merchandise {
                    ... on ProductVariant {
                      product {
                        title
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    `,
        variables: {
            cartId: cartId,
            lines: [
                {
                    merchandiseId: merchandiseId,
                    quantity: 1
                }
            ]
        }
    });
}

Just thought I'd share to help other folks beating their head against a wall like I have the last few days :)

endigo9740 avatar Jul 07 '23 17:07 endigo9740