Create your own ecommerce website and start selling successfully with ShopWired today

Create your ecommerce website on ShopWired today.
Start today with 14 days free

Create your own ecommerce website and start selling successfully with ShopWired today

Create your ecommerce website on ShopWired today.
Start today with 14 days free

How to add structured data to the products on your theme

Theme Customisations - Coding
3rd March 2021

Before you proceed:
The following article contains information that requires you to have some prior knowledge about coding in order to correctly implement the proposed change to your theme. If done incorrectly alterations made to your theme’s code can negatively impact your website causing visual and performance issues. Therefore, we strongly recommend that you only undertake this effort if you are completely confident that you know what you’re doing. If you have no coding experience at all, aren’t entirely comfortable following the instructions in this article or if you simply don’t understand the instructions then we recommend you instead contact our partners, Coding Masters, who will complete the work for you for a set fee.


You can add code to your theme's files to provide Google, and other search engines/providers, with detailed information about your products (in a structured format), by following the instructions below.

More information about Structured Data can be found on Google's website here and on the schema.org/Product website here.


Adding the code

You'll need to use the page editor to make changes to the product.twig file for your theme.

Open the product.twig file.

Scroll to the bottom of the file. Right before the {% endblock %} piece of code, add the code shown below.

<script type="application/ld+json">
{
    "@context": "http://schema.org/",
    "@type": "Product",
    "name": "{{ product.title|replace('"', "'") }}",
    "image": "https:{{ product.photo_url }}",
    "description": "{{ product.description|striptags|replace('"', "'") }}",
    {% if product.mpn %}
          "mpn": "{{ product.mpn|replace('"', "'") }}",
    {% endif %}
    {% if product.gtin %}
          "gtin": "{{ product.gtin|replace('"', "'") }}",
    {% endif %}
    {% if product.brand %}
          "brand": {
             "@type": "Thing",
             "name": "{{ product.brand.title|replace('"', "'") }}"
          },
    {% endif %}
    {% if product.sku %}
          "sku": {{ product.sku|json_encode|raw }},
     {% endif %}
     {% if reviews_enabled %}
             "aggregateRating": {
                    "@type": "AggregateRating",
                    "ratingValue": "{{ product.rating }}",
                    "reviewCount": "{{ product.reviews|length }}"
             },
     {% endif %}
     {% if product.variations %}
            "offers": [
                        {% for variation in product.variations %}
                                    {
                                    "@type": "Offer",
                                    "price": "{{ variation.price }}",
                                    "priceCurrency": "GBP",
                                    "priceValidUntil": "2020-11-05",
                                    "itemCondition": "http://schema.org/NewCondition",
                                    "url": "{{ product.url }}",
                                    {% if variation.sku %}
                                                "sku": "{{ variation.sku }}",
                                    {% endif %}
                                    {% if variation.gtin %}
                                                "gtin": "{{ variation.gtin }}",
                                    {% endif %}
                                    {% if variation.mpn %}
                                                "mpn": "{{ variation.mpn|replace('"', "'") }}",
                                    {% endif %}
                                    {% if variation.in_stock %}
                                                "availability": "https://schema.org/InStock",
                                    {% else %}
                                                "availability": "https://schema.org/OutOfStock",
                                    {% endif %}
                                    "seller": {
                                       "@type": "Organization",
                                       "name": "{{ global.business.name }}" }
                                    }{% if loop.last %}{% else %},{% endif %}
                        {% endfor %}
          ]
{% else %}
"offers": {
     "@type": "Offer",
     "priceCurrency": "GBP",
          {% if product.sale_price > 0 %}
          "price": "{{ product.sale_price }}",
          {% else %}
          "price": "{{ product.price }}",
          {% endif %}
     "url": "{{ product.url }}",
     "priceValidUntil": "2020-11-05",
     "itemCondition": "http://schema.org/NewCondition",
     {% if product.in_stock %}
            "availability": "https://schema.org/InStock",
     {% endif %}
     "seller": {
        "@type": "Organization",
        "name": "{{ global.business.name }}"
     }
   }
   {% endif %}
}
</script>

As shown in the example below.

You can use Google's Structured Data Testing Tool to test that the code is working and has been added correctly.