Looking for an improved Liquid code snippet for generating Product schema markup with Reviews

dealnest
Tourist
21 0 3

 

{% if product.reviews.size > 0 %}
{% assign review = product.reviews.first %}
{% assign author = review.author %}
{% assign datePublished = review.created_at | date: "%Y-%m-%d" %}
{% assign reviewRatingValue = review.rating %}
{% assign bestRating = 5 %}
{% assign worstRating = 1 %}

<script type="application/ld+json">
{
"@context": "https://schema.org/",
"@type": "Product",
"name": "{{ product.title }}",
"image": "{{ product.featured_image.src | img_url: 'large' }}",
"description": "{{ product.description | strip_html | escape }}",
"sku": "{{ product.sku }}",
"mpn": "{{ product.metafields.global.manufacturer_part_number }}",
"brand": {
"@type": "Brand",
"name": "{{ product.vendor }}"
},
"offers": {
"@type": "Offer",
"price": "{{ product.price | money_without_currency }}",
"priceCurrency": "{{ shop.currency }}",
"availability": "{% if product.available %}https://schema.org/InStock{% else %}https://schema.org/OutOfStock{% endif %}",
"seller": {
"@type": "Organization",
"name": "{{ shop.name }}"
}
},
"review": {
"@type": "Review",
"author": {
"@type": "Person",
"name": "{{ author }}"
},
"datePublished": "{{ datePublished }}",
"description": "{{ review.body | strip_html | escape }}",
"reviewRating": {
"@type": "Rating",
"ratingValue": "{{ reviewRatingValue }}",
"bestRating": "{{ bestRating }}",
"worstRating": "{{ worstRating }}"
}
}
}
</script>
{% endif %}

 

 

Replies 2 (2)

Relynjon
Shopify Partner
128 19 21

Here's an improved Liquid code snippet for generating product schema markup with reviews:

 

{% assign product = product %}
{% assign product_url = shop.url | append: '/products/' | append: product.handle %}
{% assign product_price = product.price | times: 100 %}
{% assign product_image_url = product.featured_image.src | img_url: 'large' %}
{% assign product_rating_value = product.metafields.yotpo_overall_score or '' %}
{% assign product_rating_count = product.metafields.yotpo_total_reviews or '' %}
{% assign product_description = product.description | strip_html | truncate: 100 %}

<script type="application/ld+json">
{
  "@context": "http://schema.org/",
  "@type": "Product",
  "name": "{{ product.title }}",
  "image": "{{ product_image_url }}",
  "description": "{{ product_description }}",
  "brand": "{{ product.vendor }}",
  "sku": "{{ product.sku }}",
  "offers": {
    "@type": "Offer",
    "priceCurrency": "{{ shop.currency }}",
    "price": "{{ product_price }}",
    "availability": "{% if product.available %}InStock{% else %}OutOfStock{% endif %}"
  },
  "aggregateRating": {
    "@type": "AggregateRating",
    "ratingValue": "{{ product_rating_value }}",
    "reviewCount": "{{ product_rating_count }}"
  },
  "review": [
    {% for review in product.metafields.yotpo_reviews %}
    {
      "@type": "Review",
      "reviewRating": {
        "@type": "Rating",
        "ratingValue": "{{ review.score }}"
      },
      "author": {
        "@type": "Person",
        "name": "{{ review.user_display_name }}"
      },
      "datePublished": "{{ review.date }}",
      "description": "{{ review.content }}",
      "name": "{{ review.title }}"
    }{% unless forloop.last %},{% endunless %}
    {% endfor %}
  ],
  "url": "{{ product_url }}"
}
</script>

 

This code snippet includes:

  • The product's name, image, description, brand, SKU, and price information.
  • The product's availability status (in stock/out of stock).
  • The product's aggregate rating (if available), which includes the rating value and review count.
  • A list of the product's individual reviews, including the review's rating, author, date published, description, and name.
  • The product's URL.

Note: This code snippet assumes that your store is using Yotpo for product reviews. If you're using a different review app, you'll need to adjust the code accordingly.

umair1400
Tourist
5 0 0

i am using trust pilot app for reviews and ratings.can you please edit the code for trustpilot app so i can use in ny store.and help will be really appreciated.