用途
商品ページで Google の「商品スニペット」や「リッチリザルト」表示を狙うときに必須。複数バリアント(色・サイズ等)がある場合も個別に構造化データを出力でき、Google Shopping や検索結果で価格・在庫・品番が正確に表示される。
設置場所
snippets/json-ld-product.liquid に配置し、templates/product.liquid または sections/main-product.liquid の `</body>` タグ直前で `{% render 'json-ld-product' %}` で呼び出す。
注意点
price は money_without_currency で通貨記号を除き、remove: ',' でカンマを削除する必要があるため、ストア通貨が円の場合も数値のみで出力すること。barcode が SKU と異なる場合は管理画面で明確に設定してから本番デプロイを推奨。複数バリアントがあるときは offers が配列になり、additionalProperty の item_group_id で同一商品グループとして Google に認識させるため、この構造を保つ。Google Search Console の URL 検査ツールと Structured Data Testing Tool でエラーが出ていないか本番公開前に必ず確認する。
コード
133 行 / liquid{%- comment -%}
Based from https://feedarmy.com/kb/shopify-microdata-for-google-shopping/
Basic usage : {% render 'json-ld-product' %} in product template
{%- endcomment -%}
{%- 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="FeedArmy">
{
"@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 fa_current_variant.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 -%}
{%- endif -%}
}
</script>