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.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Re: Product REST API | Product with no variants

Solved

Product REST API | Product with no variants

DMiradakis
Tourist
7 0 10

Shopify Community,

I have a simple question to ask regarding the REST API and Product data structure that I believe I already understand, but I want to confirm with the community that my understanding is indeed correct.

Am I correct in stating that when you retrieve a Shopify product's information from the REST API, and the product does not have any variants, that there will always be a variant field returned in that REST API call with one member in the variants JSON array? Is this what we call the default variant for a product, even if the product is not marked as having any variants itself in the Shopify admin?

I believe the answer to this question is yes, but I would like to hear confirmation from anyone else to ensure my understanding is indeed correct. The GraphQL API is a bit more detailed in its product information returned than the REST API is.

Thank you!

Best Regards,

Daniel

Accepted Solution (1)

michaeltheodore
Explorer
59 6 9

This is an accepted solution.

Hi Daniel

You are correct. A product with no variants has a single default variant. This is so that when you add to cart you add the product variant id.

For example if you're using GraphQL in the following:

 

 

{
  products(first : 100) {
    edges {
      node {
        variants(first: 10) {
            edges {
              node {
                id
              }
            } 
        }
      }
    }
  }
}

 

You add the variant id to the cart. In the above you would do a map in JS to add each variant id if there are many.

For example if you're using React you do

 

function handleAddToCart() {
  products.edges.node.variants.edges.map(item => addVariantId(item.variant.id))
}

 

 

If the product only has one variant (the default one) it's like so:

 

function handleAddToCart() {
  products.edges.node.variants.edges[0].node.id // => single variant
}

 

 

View solution in original post

Reply 1 (1)

michaeltheodore
Explorer
59 6 9

This is an accepted solution.

Hi Daniel

You are correct. A product with no variants has a single default variant. This is so that when you add to cart you add the product variant id.

For example if you're using GraphQL in the following:

 

 

{
  products(first : 100) {
    edges {
      node {
        variants(first: 10) {
            edges {
              node {
                id
              }
            } 
        }
      }
    }
  }
}

 

You add the variant id to the cart. In the above you would do a map in JS to add each variant id if there are many.

For example if you're using React you do

 

function handleAddToCart() {
  products.edges.node.variants.edges.map(item => addVariantId(item.variant.id))
}

 

 

If the product only has one variant (the default one) it's like so:

 

function handleAddToCart() {
  products.edges.node.variants.edges[0].node.id // => single variant
}