Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

nuxt.js - nuxt-shopify: cannot add productvariant to checkout (invalid global id)

I am building a headless shopify website using shopify-nuxt. I can already fetch products and productvariants and create a checkout to get the checkoutID.

Now, I want to add items to the cart to complete the checkout. The ID's I am getting of my productvariant looks like this:

 {
        "id": 37347204038813,
        "product_id": 5909234516125,
        "inventory_item_id": 39367677640861,
        "admin_graphql_api_id": "gid://shopify/ProductVariant/37347204038813"
 },

Now, in my Vuex store, when I want to add a lineItem, I am using the method like this:

  async addCartItem(state, {selectedVariantId}) {
    const lineItemsToAdd = [
      {
        variantId: selectedVariantId, // either id, product_id or inventory_item_id
        quantity: 1
      }];
console.log(lineItemsToAdd) // [{variantId: 5909234516125, quantity: 1}]

    
    await this.$shopify.checkout.addLineItems(this.app.$checkout.getCheckoutId(), lineItemsToAdd)
      .then(checkout => {
        // The Method is not going here.
        state.commit('updateCheckout', checkout);
      }).catch((e) => {
        console.log(e);
      });
  },

When I try to add a lineItem to the checkout, using the sdk I am getting the following error:

Error: [{"message":"Variable $lineItems of type [CheckoutLineItemInput!]! was provided invalid value for 0.variantId (Invalid global id `5909234516125`)","locations":[{"line":1,"column":3245}],"extensions":{"value":[{"variantId":5909234516125,"quantity":1}],"problems":[{"path":[0,"variantId"],"explanation":"Invalid global id `5909234516125`","message":"Invalid global id `5909234516125`"}]}}]

I noticed, that the ID of my productvariant looks different from the ID of the nuxt-shopify documentation example. But I do not know, how to get the "correct" id, because fetching the product with the wrong ids does not work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After a lot of research, I found out how to get the correct ID. You have to fetch the property admin_graph_api_id and enocde it with btoa like this:

btoa(this.selectedVariantId)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...