1. Home
  2. Other
  3. Displaying review rich snippets in your Shopify store with json ld

Displaying review rich snippets in your Shopify store with json ld

Case 1: No JSON ld included in your theme.

We have prepared an easy to use default JSON ld snippet for you. Paste the ID snippet into your product.liquid Shopify template. You can ask us for help via live chat in the bottom right corner of the Opinew Dashboard if required.

<script>
     
 var metafield = document.querySelector('[data-opw-prodreviews]').getAttribute('data-opw-prodreviews')

 if (metafield){
    metafield = JSON.parse(metafield);
 }

var opinewJson = {
  "@context": "http://schema.org/",
  "@type": "Product",
  "name": {{ product.title | json }},
  "url": {{ shop.url | append: product.url | json }},
  {% if product.first_available_variant.barcode.size == 13 %}
    "gtin13":"{{ product.variants.first.barcode }}",
  {% elsif product.first_available_variant.barcode.size == 12 %}
    "gtin12":"{{ product.variants.first.barcode }}",
  {% elsif product.first_available_variant.barcode.size == 8 %}
    "gtin8":"{{ product.variants.first.barcode }}",
  {% endif %}
  {%- if product.featured_image -%}
    {%- assign image_size = product.featured_image.width | append: 'x' -%}
    "image": [
      {{ product.featured_image.src | img_url: image_size | prepend: "https:" | json }}
    ],
  {%- endif -%}
  "description": {{ product.description | strip_html | json }},
  {%- if product.selected_or_first_available_variant.sku != blank -%}
    "sku": {{ product.selected_or_first_available_variant.sku | json }},
  {%- else -%}
      {% if product.variants.first.sku != blank %}
        "sku": "{{ product.variants.first.sku }}",
      {% else %}
        "sku": "{{ product.variants.first.id }}",
      {% endif %}
  {%- endif -%}
  "brand": {
    "@type": "Thing",
    "name": {{ product.vendor | json }}
  },
  {%- if product.metafields.opinew_metafields.reviews_count and product.metafields.opinew_metafields.reviews_count != "0" -%} 
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": {{ product.metafields.opinew_metafields.reviews_average }},
"ratingCount": {{ product.metafields.opinew_metafields.reviews_count }}
},
  "review": {
        "@type": "Review",
        "reviewRating": {
          "@type": "Rating",
          "ratingValue": metafield["reviews"]["recent"][0]["star_rating"]
        },
        "author": {
          "@type": "Person",
          "name": metafield["reviews"]["recent"][0]["user_name"]
        },
        "reviewBody": metafield["reviews"]["recent"][0]["body"]
    }, 
{%- endif -%}

  "offers": [
    {%- for variant in product.variants -%}
      {
        "@type" : "Offer",
        {%- if variant.sku != blank -%}
          "sku": {{ variant.sku | json }},
        {%- endif -%}
        "availability" : "http://schema.org/{% if product.available %}InStock{% else %}OutOfStock{% endif %}",
        "priceValidUntil" : "{{ 'now' | date: '%s' | plus: 31536000 | date: '%Y-%m-%d' | uri_encode | replace:'+','%20' }}",
        "price" : {{ variant.price | divided_by: 100.00 | json }},
        "priceCurrency" : {{ cart.currency.iso_code | json }},
        "url" : {{ shop.url | append: variant.url | json }}
      }{% unless forloop.last %},{% endunless %}
    {%- endfor -%}
  ]
}

var el = document.createElement('script');
el.type = 'application/ld+json';
el.text = JSON.stringify(opinewJson);
document.querySelector('body').appendChild(el);
</script>

Case 2: You already have a JSON ld snippet included in your Shopify theme.

You can add this ID snippet code to make our reviews data work with your existing JSON ld

{% if product.metafields.opinew_metafields.reviews_count and product.metafields.opinew_metafields.reviews_count != "0" %} 
    "aggregateRating": {
    "@type": "AggregateRating",
    "ratingValue": {{ product.metafields.opinew_metafields.reviews_average }},
    "ratingCount": {{ product.metafields.opinew_metafields.reviews_count }}
},
{% endif %}

Case 3: You’re using a JSON ld app

So far those are the Shopify apps that support Opinew:

If your app doesn’t support Opinew yet, please get in touch with their support. Integrating with Opinew is very easy and should be quick for them to implement!

We can help with installation

Contact support via live chat in the bottom right corner of Opinew Dashboard and we will help you install the JSON ld codes.

Updated on August 10, 2021

Was this article helpful?

Related Articles

Leave a Comment