Shopify 产品页面的 Schema 结构化数据输出错误及其解决方案(2022 Dawn 模板)

在阅读本文以及使用本文中提及的解决方案前,请先阅读之前的两篇基础文章:

你可能会在 Google Search Console 中看到的错误:

  • Missing field “aggregateRating” (optional)
  • Missing field “review” (optional)
  • Invalid object type for field “brand” (optional)
  • No global identifier provided (e.g., gtin, mpn, isbn) (optional)
  • Missing field “priceValidUntil” (optional)

解决方案

  • Missing field “aggregateRating” (optional)
  • Missing field “review” (optional)

这两个问题的解决方案参考:《Shopify 产品 Review 的 SEO》

Invalid object type for field “brand” (optional)

Shopify 输出了 Brand,但它的类型是 Thing,修改为 Brand 即可,即把如下代码:

    "brand": {
      "@type": "Thing",
      "name": {{ product.vendor | json }}
    },

修改为:

    "brand": {
      "@type": "Brand",
      "name": {{ product.vendor | json }}
    },

No global identifier provided (e.g., gtin, mpn, isbn) (optional)

这个错误的意思是没有找到 GTIN 编码,添加以下代码:

    {%- 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 -%}

参考笔记:

Missing field “priceValidUntil” (optional)

这个错误的意思是需要价格的有效期,添加以下代码:

"priceValidUntil": "{{ 'now' | date: '%s' | plus: 31536000 | date: '%Y-%m-%d' | uri_encode | replace:'+','%20' }}"

含义为在当前时间的基础上加 1 年(31536000 秒)的时间,作为价格有效期。

完整的代码

最终在 main-product.liquid 中有如下代码,我们添加或修改的部分以红色标注:

<script type="application/ld+json">
  {
    "@context": "http://schema.org/",
    "@type": "Product",
    "@id": {{ canonical_url | append: '#product' | json }},
    "name": {{ product.title | json }},
    "url": {{ shop.url | append: product.url | json }},
    {% if seo_media -%}
      {%- assign media_size = seo_media.preview_image.width | append: 'x' -%}
      "image": [
        {{ seo_media | img_url: media_size | prepend: "https:" | json }}
      ],
    {%- endif %}
    "description": {{ product.description | strip_html | json }},
    {% if product.selected_or_first_available_variant.sku != blank -%}
      "sku": {{ product.selected_or_first_available_variant.sku | json }},
    {%- endif %}
    "brand": {
      "@type": "Brand",
      "name": {{ product.vendor | json }}
    },
    {%- 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 -%}
    "offers": [
      {%- for variant in product.variants -%}
        {
          "@type" : "Offer",
          {%- if variant.sku != blank -%}
            "sku": {{ variant.sku | json }},
          {%- endif -%}
          "availability" : "http://schema.org/{% if variant.available %}InStock{% else %}OutOfStock{% endif %}",
          "price" : {{ variant.price | divided_by: 100.00 | json }},
          "priceCurrency" : {{ cart.currency.iso_code | json }},
          "url" : {{ shop.url | append: variant.url | json }},
          "priceValidUntil": "{{ 'now' | date: '%s' | plus: 31536000 | date: '%Y-%m-%d' | uri_encode | replace:'+','%20' }}"
        }{% unless forloop.last %},{% endunless %}
      {%- endfor -%}
    ]
  }
</script>

京ICP备12052177号-1