Skip to content

Instantly share code, notes, and snippets.

@blueprintmrk
Created September 3, 2020 20:38
Show Gist options
  • Save blueprintmrk/c434ec74d64dbb0894d7ea69f2c8e69f to your computer and use it in GitHub Desktop.
Save blueprintmrk/c434ec74d64dbb0894d7ea69f2c8e69f to your computer and use it in GitHub Desktop.
<!-- 1.0 -->
{% assign fa_current_variant = product.selected_or_first_available_variant %}
{%- assign fa_variant_count = product.variants | size -%}
{%- assign fa_count = 0 -%}
<script type="application/ld+json" data-creator_name="NatureCity">
{
"@context": "http://schema.org/",
"@type": "Product",
"name": "{{ product.title | strip_html | escape }}",
"url": "{{ shop.url }}{{ product.url }}",
{%- if product.variants.first.sku != blank -%}
"sku": "{{ product.variants.first.sku }}",
{%- else -%}
"sku": "{{ product.variants.first.id }}",
{%- endif -%}
{%- if product.variants.first.barcode.size == 12 -%}
"gtin12": {{ product.variants.first.barcode }},
{%- endif -%}
{%- if product.variants.first.barcode.size == 13 -%}
"gtin13": {{ product.variants.first.barcode }},
{%- endif -%}
{%- if product.variants.first.barcode.size == 14 -%}
"gtin14": {{ product.variants.first.barcode }},
{%- endif -%}
"productID": "{{ product.id }}",
"brand": {
"@type": "Thing",
"name": "{{ product.vendor | escape }}"
},
"description": {{ product.description | strip_html | json }},
"image": "https:{{ product.featured_image.src | img_url: 'grande' }}",
{%- if product.variants -%}
{%- assign fa_count = fa_count | plus: 1 -%}
"offers": {% if product.variants.size > 1 %}[{% endif %}
{
"@type" : "Offer",
"priceCurrency": "{{ cart.currency.iso_code }}",
"price": "{{ fa_current_variant.price | money_without_currency | strip_html | remove: ',' }}",
"itemCondition" : "http://schema.org/NewCondition",
"availability" : "http://schema.org/{% if product.available %}InStock{% else %}OutOfStock{% endif %}",
"url" : "{{ shop.url }}{{ fa_current_variant.url }}",
{%- if fa_current_variant.image -%}
{% assign variant_image_size = fa_current_variant.image.width | append: 'x' %}
"image": "https:{{ fa_current_variant.image.src | img_url: variant_image_size }}",
{%- else -%}
"image": "https:{{ product.featured_image.src | img_url: 'grande' }}",
{%- endif -%}
{%- if fa_current_variant.title != 'Default Title' -%}
"name" : "{{ product.title | strip_html | escape }} - {{ fa_current_variant.title | escape }}",
{%- else -%}
"name" : "{{ product.title | strip_html | escape }}",
{%- endif -%}
{% if fa_current_variant.barcode.size == 12 %}
"gtin12": {{ fa_current_variant.barcode }},
{%- endif -%}
{%- if fa_current_variant.barcode.size == 13 -%}
"gtin13": {{ fa_current_variant.barcode }},
{%- endif -%}
{%- if fa_current_variant.barcode.size == 14 -%}
"gtin14": {{ fa_current_variant.barcode }},
{%- endif -%}
{%- if fa_current_variant.sku != blank -%}
"sku": "{{ fa_current_variant.sku }}",
{%- else -%}
"sku": "{{ fa_current_variant.id }}",
{%- endif -%}
{%- if product.description != blank -%}
"description" : {{ product.description | strip_html | json }},
{%- endif -%}
"priceValidUntil": "{{ 'now' | date: '%s' | plus: 31536000 | date: '%Y-%m-%d' | uri_encode | replace:'+','%20' }}"
}{%- if product.variants.size != 1 -%},{%- endif -%}
{%- for variant in product.variants -%}
{%- if variant != product.selected_or_first_available_variant -%}
{%- assign fa_count = fa_count | plus: 1 -%}
{
"@type" : "Offer",
"priceCurrency": "{{ cart.currency.iso_code }}",
"price": "{{ variant.price | money_without_currency | strip_html | remove: ',' }}",
"itemCondition" : "http://schema.org/NewCondition",
"availability" : "http://schema.org/{% if variant.available %}InStock{% else %}OutOfStock{% endif %}",
"url" : "{{ shop.url }}{{ variant.url }}",
{%- if variant.image -%}
{% assign variant_image_size = variant.image.width | append: 'x' %}
"image": "http:{{ variant.image.src | img_url: variant_image_size }}",
{%- else -%}
"image": "https:{{ product.featured_image.src | img_url: 'grande' }}",
{%- endif -%}
{%- if variant.title != 'Default Title' -%}
"name" : "{{ product.title | strip_html | escape }} - {{ variant.title | escape }}",
{%- else -%}
"name" : "{{ product.title | strip_html | escape }}",
{%- endif -%}
{%- if variant.barcode.size == 12 -%}
"gtin12": {{ variant.barcode }},
{%- endif -%}
{%- if variant.barcode.size == 13 -%}
"gtin13": {{ variant.barcode }},
{%- endif -%}
{%- if variant.barcode.size == 14 -%}
"gtin14": {{ variant.barcode }},
{%- endif -%}
{%- if variant.sku != blank -%}
"sku": "{{ variant.sku }}",
{%- else -%}
"sku": "{{ variant.id }}",
{%- endif -%}
{%- if product.description != blank -%}
"description" : {{ product.description | strip_html | json }},
{%- endif -%}
"priceValidUntil": "{{ 'now' | date: '%s' | plus: 31536000 | date: '%Y-%m-%d' | uri_encode | replace:'+','%20' }}"
}
{%- if fa_count < fa_variant_count -%}
{%- unless forloop.last -%},{%- endunless -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- if product.variants.size > 1 -%}],
"additionalProperty": [{
"@type": "PropertyValue",
"propertyID": "item_group_id",
"value": "{{ product.id }}"
}]
{%- endif -%}
{%- if product.metafields.spr.reviews -%}
,"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": {{ product.metafields.spr.reviews | split: '"ratingValue": "' | last | split: '"' | first | plus: 0 }},
"ratingCount": {{ product.metafields.spr.reviews | split: '"reviewCount": "' | last | split: '"' | first | plus: 0 }}
}
{%- endif -%}
{%- endif -%}
}
</script>
<!-- Copyright NatureCity 2020 Version 1.0 --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment