Simplified product template snippet that outputs product structured data from the theme, including the rating only when the product has one, using the same data as the visible stars.
Product template · ratings markup in the themeSimplified
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Product",
{%- assign count = product.metafields.reviews.rating_count.value -%}
{%- if product.metafields.reviews.rating.value != blank and count > 0 -%}
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": {{ product.metafields.reviews.rating.value.rating | json }},
"reviewCount": {{ count | json }}
},
{%- endif -%}
"name": {{ product.title | json }}
}
</script>
<script type="application/ld+json">
{
"@type": "Product",
{% assign r = product.metafields.reviews %}
{% assign count = r.rating_count.value %}
{% if count > 0 %}
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue":
{{ r.rating.value.rating | json }},
"reviewCount": {{ count | json }}
},
{% endif %}
"name": {{ product.title | json }}
}
</script>
Values come from the same data as the stars shown on the page, so the two always match.