{{!-- Product: include in product.html page template --}}

{{inject 'productDescription' product.description}}
<script id="schema-product">
  var description = JSON.parse({{jsContext}}).productDescription;

  var schema = document.createElement('script');
  schema.type = 'application/ld+json';
  schema.text = JSON.stringify({
    "@context": "http://schema.org/",
    "@type": "Product",
    "name": "{{product.title}}",
    "image": "{{getImage product.main_image}}",
    "description": description,
    "sku": "{{product.sku}}",
    "brand": {
      "@type": "Thing",
      "name": "{{product.brand.name}}"
    },
    {{#if product.reviews.list}}
      "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "{{product.rating}}",
        "reviewCount": "{{product.num_reviews}}"
      },
    {{/if}}
    "offers": {
      "@type": "Offer",
      "priceCurrency": "{{currency_selector.active_currency_code}}",
      "price": "{{#if product.price.without_tax}}{{product.price.without_tax.value}}{{/if}}{{#if product.price.with_tax}}{{#unless product.price.without_tax}}{{product.price.with_tax.value}}{{/unless}}{{/if}}",
      "itemCondition": "{{#if product.condition '===' 'New'}}http://schema.org/NewCondition{{/if}}{{#if product.condition '===' 'Used'}}http://schema.org/UsedCondition{{/if}}{{#if product.condition '===' 'Refurbished'}}http://schema.org/RefurbishedCondition{{/if}}",
      "availability": "{{#unless product.out_of_stock_message}}http://schema.org/InStock{{else}}http://schema.org/OutOfStock{{/unless}}",
      "seller": {
        "@type": "Organization",
        "name": "{{settings.store_name}}"
      }
    }
  });

  document.querySelector('body').appendChild(schema);

  var script = document.getElementById('schema-product');
  script.parentElement.removeChild(script);
</script>
