Register now - Learn how Tecovas roped in success with Sanity and Shopify ๐Ÿค 

How to add product categories in a content model.

6 replies
Last updated: Mar 6, 2021
Hello ๐Ÿ‘‹Hoping someone can help me out with my content modelling.
Context: In my (singleton) settings document I want to have an array of strings to add product categories. Then, in another document, I want to have an array with predefined options AKA the product categories.

Does anyone have a suggestion of how to do this, other than having to define the product categories as documents?
Mar 6, 2021, 4:12 PM
Hi User! If Iโ€™m understanding you correctly it sounds like a reference or array of references should work (depending on if you want to allow one product category per product or many ).
Once your categories are set up in your singleton document, you could use this in your product schema if you want to permit one category:

  name: "category",
  title: "Category",
  type: "reference",
  to: [{ type: "category" }],
Or this if you want to allow more than one:

  title: 'Categories',
  name: 'categories',
  type: 'array',
  of: [
      type: 'reference',
      to: [{type: 'category'}]

Mar 6, 2021, 4:21 PM
I was hoping I didn't have to make a category as a document type, but instead be able to reference the field categories (that would be an array of strings) within the settings document. In my mind that would look nicer in the studio, since I could format it as tags, but I guess this will work well too ๐Ÿ˜Š thank you!
Mar 6, 2021, 4:42 PM
Why can't you have a custom input which does an API lookup of your singleton document and fetches the category field contents and displays them as check boxes?
Mar 6, 2021, 8:35 PM
I'm sure I can, but I'm rather new to Sanity, so it would be great if you could guide me to the relevant section of the docs or explain to me a bit more in detail :)
Mar 6, 2021, 9:23 PM
The way you could go about doing it is creating the category array of strings in your singleton schema. Then in your other schema creating a categories field which is controlled with a custom input widget here's someone doing something a bit similar
Mar 6, 2021, 9:32 PM
Thank you, I'll look into it in the morning ๐Ÿ˜Š
Mar 6, 2021, 9:39 PM

Sanityโ€“ build remarkable experiences at scale

Sanity is a modern 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?