How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

Filter references based on selected product in nested variants array

2 repliesLast updated: Nov 30, 2025

Hi!I got an array products and within those products there are an array of variants.

I need to filter reference based on the selected product. Since I need to be able to choose several variants per product I cant seem to find a way to get the selected product.

Anyone know a solution to this?
AI Update

I can see you're trying to filter variant references based on a selected product within an array structure. This is a common challenge when working with nested arrays in Sanity Studio.

The key issue is that when you're in the variants array, you need to access the parent product field to filter which variants should be available. The tricky part is that parent gives you the variants array itself, so you need to traverse up through the document to find the product reference.

Based on the conditional fields documentation and solutions from the Sanity community, here's how to solve this:

defineField({
  name: 'products',
  title: 'Products',
  type: 'array',
  of: [
    defineArrayMember({
      name: 'productWithSelectedVariants',
      title: 'Product with selected variants',
      type: 'object',
      fields: [
        defineField({
          name: 'product',
          title: 'Product',
          type: 'reference',
          to: [{ type: 'product' }],
          weak: true,
        }),
        defineField({
          name: 'variants',
          title: 'Variants',
          type: 'array',
          of: [
            defineArrayMember({
              name: 'variant',
              title: 'Variant',
              type: 'reference',
              weak: true,
              to: [{ type: 'productVariant' }],
              options: {
                filter: ({ parent, document }) => {
                  // Find the product object that contains this variants array
                  const productObj = document.products?.find(
                    (p) => p.variants === parent
                  )
                  
                  // Get the product reference ID
                  const productID = productObj?.product?._ref
                  
                  if (!productID) {
                    return {
                      filter: '_type == "productVariant"',
                      params: {}
                    }
                  }
                  
                  return {
                    filter: '_type == "productVariant" && references($productID)',
                    params: {
                      productID,
                    },
                  }
                },
              },
            }),
          ],
        }),
      ],
    }),
  ],
})

How this works:

Important notes:

If your variant documents don't have a reference back to the product, you'll need to adjust the GROQ filter to match based on other fields like a productId string field or similar identifier that connects variants to their products.

Show original thread
2 replies

Was this answer helpful?

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Related contributions