Skip to content

Instantly share code, notes, and snippets.

@tasz
Created August 28, 2020 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tasz/9c761bb6793e88126dfe46510dd237c6 to your computer and use it in GitHub Desktop.
Save tasz/9c761bb6793e88126dfe46510dd237c6 to your computer and use it in GitHub Desktop.
Google Shema warning fix shopify product
{%- if request.page_type == 'product' -%}
{%- assign days_product_price_valid_until = 10 | times: 86400 -%}
{%- assign gtin_option = 'gtin' -%}
{%- if product.selected_or_first_available_variant.barcode != blank -%}
{%- assign is_barcode_available = true -%}
{%- assign gtin_string_length = product.selected_or_first_available_variant.barcode | size -%}
{%- if gtin_string_length == 8 or gtin_string_length == 12 or gtin_string_length == 13 or gtin_string_length == 14 -%}
{%- assign is_valid_gtin_length = true -%}
{%- assign gtin_option = gtin_option | append: gtin_string_length -%}
{%- endif -%}
{%- endif -%}
{%- assign has_reviews = false -%}
{%- assign rating_value = blank -%}
{%- assign review_count = blank -%}
{%- if product.metafields.spr.reviews != blank -%}
{%- assign rating_value_review_array = product.metafields.spr.reviews | split: 'meta itemprop="ratingValue" content="' -%}
{%- assign rating_value_arr = rating_value_review_array[1] |split: '"' -%}
{%- assign rating_value = rating_value_arr[0] -%}
{%- assign review_count_str_array = product.metafields.spr.reviews | split: 'meta itemprop="reviewCount" content="' -%}
{%- assign review_count_arr = review_count_str_array[1] | split: '"' -%}
{%- assign review_count = review_count_arr[0] -%}
{%- unless review_count == blank -%}
{%- assign has_reviews = true -%}
{%- endunless -%}
{%- endif -%}
{%- capture main_entity_microdata -%}
"@type": "Product",
"offers": [
{%- for variant in product.variants -%}
{
"@type": "Offer",
"name": {{ variant.title | json }},
"availability": {%- if variant.available -%}"https://schema.org/InStock"{%- else -%}"https://schema.org/OutOfStock"{%- endif -%},
"price": {{ variant.price | money_without_currency | json }},
"priceCurrency": {{ shop.currency | json }},
"priceValidUntil": "{{ 'now' | date: '%s' | plus: days_product_price_valid_until | date: '%Y-%m-%d'}}",
{%- if variant.sku != blank -%}
"sku": {{ variant.sku | json }},
{%- endif -%}
"url": "{{ request.path }}{{ variant.url }}"
}{% unless forloop.last %},{% endunless %}
{%- endfor -%}
],
{%- if is_barcode_available and is_valid_gtin_length %}
"{{gtin_option}}": {{ product.selected_or_first_available_variant.barcode | json }},
"productId": {{ product.selected_or_first_available_variant.barcode | json }},
{%- elsif is_barcode_available %}
"mpn": {{ product.selected_or_first_available_variant.barcode | json }},
"productId": {{ product.selected_or_first_available_variant.barcode | json }},
{%- endif %}
{%- if has_reviews -%}
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "{{ rating_value }}",
"reviewCount": "{{ review_count }}"
},
{%- endif %}
"brand": {
"name": {{ product.vendor | json }}
},
"name": {{ product.title | json }},
"description": {{ product.description | strip_html | json }},
"category": {{ product.type | json }},
"url": "{{ request.path }}{{ product.url }}",
"sku": {{ product.selected_or_first_available_variant.sku | json }},
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "5",
"bestRating": "5",
"worstRating": "1",
"ratingCount": "1"
},
"image": {
"@type": "ImageObject",
"url": "https:{{ product.featured_image | img_url: '1024x' }}",
"image": "https:{{ product.featured_image | img_url: '1024x' }}",
"name": {{ product.featured_image.alt | json }},
"width": "1024",
"height": "1024"
}
{%- endcapture -%}
{%- elsif request.page_type == 'article' -%}
{%- capture main_entity_microdata -%}
"@type": "BlogPosting",
"mainEntityOfPage": "{{ article.url }}",
"articleSection": {{ blog.title | json }},
"keywords": "{{ article.tags | join: ', ' }}",
"headline": {{ article.title | json }},
"description": {{ article.excerpt_or_content | strip_html | truncatewords: 25 | json }},
"dateCreated": "{{ article.created_at | date: '%Y-%m-%dT%T' }}",
"datePublished": "{{ article.published_at | date: '%Y-%m-%dT%T' }}",
"dateModified": "{{ article.published_at | date: '%Y-%m-%dT%T' }}",
"image": {
"@type": "ImageObject",
"url": "https:{{ article.image | img_url: '1024x' }}",
"image": "https:{{ article.image | img_url: '1024x' }}",
"name": {{ article.image.alt | json }},
"width": "1024",
"height": "1024"
},
"author": {
"@type": "Person",
"name": "{{ article.user.first_name | escape }} {{ article.user.last_name | escape }}",
"givenName": {{ article.user.first_name | json }},
"familyName": {{ article.user.last_name | json }}
},
"publisher": {
"@type": "Organization",
"name": {{ shop.name | json }}
},
"commentCount": {{ article.comments_count }},
"comment": [
{%- for comment in article.comments limit: 5 -%}
{
"@type": "Comment",
"author": {{ comment.author | json }},
"datePublished": "{{ comment.created_at | date: '%Y-%m-%dT%T' }}",
"text": {{ comment.content | json }}
}{%- unless forloop.last -%},{%- endunless -%}
{%- endfor -%}
]
{%- endcapture -%}
{%- endif -%}
{%- capture breadcrumb_entity_microdata -%}
"@type": "BreadcrumbList",
"itemListElement": [{
"@type": "ListItem",
"position": 1,
"name": {{ 'general.breadcrumb.home' | t | json }},
"item": "{{ shop.url }}"
}
{%- if request.page_type == 'product' -%}
{%- if collection -%}
,{
"@type": "ListItem",
"position": 2,
"name": {{ collection.title | json }},
"item": "{{ shop.url }}{{ collection.url }}"
}, {
"@type": "ListItem",
"position": 3,
"name": {{ product.title | json }},
"item": "{{ shop.url }}{{ product.url | within: collection }}"
}
{%- else -%}
,{
"@type": "ListItem",
"position": 2,
"name": {{ product.title | json }},
"item": "{{ shop.url }}{{ product.url }}"
}
{%- endif -%}
{%- elsif request.page_type == 'collection' -%}
,{
"@type": "ListItem",
"position": 2,
"name": {{ collection.title | json }},
"item": "{{ shop.url }}{{ collection.url }}"
}
{%- elsif request.page_type == 'blog' -%}
,{
"@type": "ListItem",
"position": 2,
"name": {{ blog.title | json }},
"item": "{{ shop.url }}{{ blog.url }}"
}
{%- elsif request.page_type == 'article' -%}
,{
"@type": "ListItem",
"position": 2,
"name": {{ blog.title | json }},
"item": "{{ shop.url }}{{ blog.url }}"
}, {
"@type": "ListItem",
"position": 3,
"name": {{ blog.title | json }},
"item": "{{ shop.url }}{{ article.url }}"
}
{%- elsif request.page_type == 'page' -%}
,{
"@type": "ListItem",
"position": 2,
"name": {{ page.title | json }},
"item": "{{ shop.url }}{{ page.url }}"
}
{%- endif -%}
]
{%- endcapture -%}
{% if main_entity_microdata != blank %}
<script type="application/ld+json">
{
"@context": "http://schema.org",
{{ main_entity_microdata }}
}
</script>
{% endif %}
{% if breadcrumb_entity_microdata != blank %}
<script type="application/ld+json">
{
"@context": "http://schema.org",
{{ breadcrumb_entity_microdata }}
}
</script>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment