Schema setup for products

If I have this schema setup in the head of my template, how do I have it loop through my products?

<script type="application/ld+json">
[ {
  "@context" : "http://schema.org",
  "@type" : "Product",
  "name" : "{{ this.name }}",
  "image" : "{{ this.image }}",
  "description" : "{{ this.description }}"
}, {
  "@context" : "http://schema.org",
  "@type" : "Product",
  "name" : "{{ this.name }}",
  "image" : "{{ this.image }}",
  "description" : "{{ this.description }}"
}, {
  "@context" : "http://schema.org",
  "@type" : "Product",
  "name" : "{{ this.name }}",
  "image" : "{{ this.image }}",
  "description" : "{{ this.description }}"
} ]
</script>

Hi @luke
Something like this might do the trick? :slight_smile:

{% component source: "Products", layout: "", limit: "999", collectionVariable: "productCollection", type: "module" %}
<script type="application/ld+json">
  [ 
{% for product in productCollection.items %}
  {
  "@context" : "http://schema.org",
  "@type" : "Product",
  "name" : "{{product.name}}",
  "image" : "{{product.SmallImage}}",
  "description" : "{{product.description}}"
  }{% unless forloop.last %},{% endunless %}
{% endfor %}  
]
</script>
1 Like

Why do you want to loop through all products in the first place? :slight_smile:
Don’t you just want it on the product page itself and fill in the product data or is there a specific reason why you want to loop through all of your products? :slight_smile:

Thanks for that mate.
At the moment I have manually filled it out but doing it this way I would have thought it would update the schema file when you add new products would it not? or is this a bad idea?
Also, the more for loops you add to a site will it put heavy slow down on it?

@luke - No worries :slight_smile:
Yeah this will automaticly add new products, but not quite sure why you want to do this on all pages. I thought you just wanted the info of the product on which product page the user is currently on? :slight_smile:

I was a little confused but now I totally get what you’re saying as this data goes on a product detail page so I don’t need a for loop.

awesome thanks again @Peter-Schmidt

1 Like

No worries mate :slight_smile: Glad I could help :slight_smile: