Issue with syncing product variants and querying nested references in Sanity Studio.

9 replies
Last updated: Aug 21, 2023
This message contains interactive elements.
Aug 21, 2023, 3:53 PM
You do need to create a document schema for your product variants. Take a look at this reference Studio for an example.
Aug 21, 2023, 4:29 PM
Thank you very much for sending this!I had taken a look before, but checking again I realized some of my schemas weren’t typed properly.

I was able to sync in all the product variants as documents, but now I am struggling to get all of the variants into the product document, so that I can eventually query and render into a dropdown.

I keep getting “Item type of reference not valid for this list”

Variants field from product document:


    {
          title: 'Variants',
          name: 'variants',
          type: 'array',
          readOnly: true,
          of: [
            {
              name: 'productVariant',
              title: 'Product variant',
              type: 'reference',
              to: [{type: 'productVariant'}],
              weak: true,
              options: {
                disableNew: true,
              },
            },
          ],
        },
Aug 21, 2023, 5:34 PM
Okay, realized I needed to get rid of ‘name’ in the reference.Now I am able to query the default variants of each product without variants, but am now getting nothing for products with actual variants.
Aug 21, 2023, 5:49 PM
This may be an issue with your GROQ query, could you share it?
Aug 21, 2023, 6:01 PM
Well, I’m having trouble with the query as well to get the default variant for products without a variant. Do you know how to query a nested reference within a reference?
const query = groq`*[_type == “page”]{..., pageProduct{..., product->}, bundles[]->}`;
Aug 21, 2023, 6:03 PM
The flow is like this, each being its own document:
Page document -> pageProduct -> productVariants
Aug 21, 2023, 6:04 PM
Yes, after the
->
you’ll need to use some brackets to dive a little deeper. Following your `product->`:

...,
product->{
  store {
    variants[]->,
    ...
  },
  ...
}

Aug 21, 2023, 6:06 PM
Ahh, got it now. Thank you very much!Actually checking now, I am able to query all the necessary information for the variants, but for whatever reason they just show up as blank in the product document in the studio.

Not ideal, but not an issue since product as can be easily chosen in the page document and then then variant info queried in the frontend.
Aug 21, 2023, 6:13 PM
In the studio, if you click on the product reference you selected in your page, it should open a new pane with that product’s information. If you’ve defined schemas for this document type, you should be able to see all of its data, as well as references to all of the variants - you will be able to click on these as well to see those documents.
Aug 21, 2023, 6:36 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?

Categorized in