Shopify二次开发之五:元字段(Metafields)

目录

解释

操作

1、添加Custom data

2、选择特定类型的数据

3、为Page配置元子段和值

4、模板访问


解释

Shopify Metafields 是一种用于存储和管理自定义数据的功能。它们允许商户在商城中的产品、订单、客户、Page等对象上添加自定义字段,以满足特定业务需求。

操作

1、添加Custom data

Shopify二次开发之五:元字段(Metafields)_第1张图片

可以为Products、Pages添加自定义数据,比如选择了Pages,那么在任何Page模板下配置了metafield的值,可以用page.metafields.custom.metafield_name.value访问添加的特定类型的元字段内容,又比如选择了Products,那么在product模板下,可以用product.metafields.custom.metafield_name.value访问为产品添加的特定数据的元字段内容,Collections同上,其它的比如Orders也有特定的用法。

就拿pages做举例

2、选择特定类型的数据

Shopify二次开发之五:元字段(Metafields)_第2张图片

为Pages添加自定义内容的数据type,可以有普通数据type:文本、日期、布尔、json、整数等,也可以有引用数据type:product、file、collection、page等。

Shopify二次开发之五:元字段(Metafields)_第3张图片

3、为Page配置元子段和值

选择一个页面

Shopify二次开发之五:元字段(Metafields)_第4张图片

show all

Shopify二次开发之五:元字段(Metafields)_第5张图片

配置元字段值

Shopify二次开发之五:元字段(Metafields)_第6张图片

4、模板访问

在page.xxx.json,通过.value访问元字段内容



{{ page.title }}

Metafield Type: single line text
Liquid Metafield Code: {% raw %}{{ page.metafields.custom.single_line_text.value }}{% endraw %}
Metafield Value: {{ page.metafields.custom.single_line_text.value }}
Metafield Type: list single line text
Liquid Metafield Code: {% raw %}{{ page.metafields.custom.list_single_line_text.value }}{% endraw %}
Metafield Value: {{ page.metafields.custom.list_single_line_text.value }}
{% assign list_line_text = page.metafields.custom.list_single_line_text %}
Accessing list_single_line_text : {% raw %}{% assign list_line_text = page.metafields.custom.list_single_line_text.value %}{% endraw %}
for loop : {% raw %}{% for line_text in list_line_text.value %}{{ line_text }}{% endfor %}{% endraw %}
    {% for line_text in list_line_text.value %}
  • {% increment idx %} :{% raw %}{{ line_text }}{% endraw %}{{ line_text }}
  • {% endfor %}
Metafield Type: multi line text
Liquid Metafield Code: {% raw %}{{ page.metafields.custom.multi_line_text.value }}{% endraw %}
Metafield Value: {{ page.metafields.custom.multi_line_text.value }}
Metafield Type: integer
Liquid Metafield Code: {% raw %}{{ page.metafields.custom.integer.value }}{% endraw %}
Metafield Value: {{ page.metafields.custom.integer.value }}
Metafield Type: true of false
Liquid Metafield Code: {% raw %}{{ page.metafields.custom.boolean.value }}{% endraw %}
Metafield Value: {{ page.metafields.custom.boolean.value }}
Metafield Type: json
Liquid Metafield Code: {% raw %}{{ page.metafields.custom.json.value }}{% endraw %}
Metafield Value: {{ page.metafields.custom.json.value }}
Metafield Type: date
Liquid Metafield Code: {% raw %}{{ page.metafields.custom.date.value }}{% endraw %}
Metafield Value: {{ page.metafields.custom.date.value }}
Metafield Type: money
Liquid Metafield Code: {% raw %}{{ page.metafields.custom.money.value }}{% endraw %}
Metafield Value: {{ page.metafields.custom.money.value }}
Metafield Type: product
Liquid Metafield Code: {% raw %}{{ page.metafields.custom.product.value }}{% endraw %}
Metafield Value: {{ page.metafields.custom.product.value }}
{% assign product_value = page.metafields.custom.product.value %}
Accessing Reference Object : {% raw %}{% assign product_value = page.metafields.custom.product.value %}{% endraw %}
  • title :{% raw %}{{ product_value.title }}{% endraw %}{{ product_value.title }}
  • featured_image :{% raw %}{{ product_value.featured_image }}{% endraw %}{{ product_value.featured_image }}
  • price :{% raw %}{{ product_value.price | money }}{% endraw %}{{ product_value.price | money }}
  • url :{% raw %}{{ product_value.url | link_to: product_value.url }}{% endraw %}{{ product_value.url | link_to: product_value.url }}
Metafield Type: list product
Liquid Metafield Code: {% raw %}{{ page.metafields.custom.list_product.value }}{% endraw %}
Metafield Value: {{ page.metafields.custom.list_product.value }}
Accessing Reference Object : {% raw %}{% assign list_product = page.metafields.custom.list_product.value %}{% endraw %}
for loop : {% raw %}{% for product_item in list_product %} {{ product_item.attr }}{% endfor %}{% endraw %}
{% assign list_product = page.metafields.custom.list_product.value %} {% for product_item in list_product %}
  • title :{% raw %}{{ product_item.title }}{% endraw %}{{ product_item.title }}
  • featured_image :{% raw %}{{ product_item.featured_image }}{% endraw %}{{ product_item.featured_image }}
  • price :{% raw %}{{ product_item.price | money }}{% endraw %}{{ product_item.price | money }}
  • url :{% raw %}{{ product_item.url | link_to: product_item.url }}{% endraw %}{{ product_item.url | link_to: product_item.url }}
{% endfor %}
Metafield Type: collection
Liquid Metafield Code: {% raw %}{{ page.metafields.custom.collection.value }}{% endraw %}
Metafield Value: {{ page.metafields.custom.collection.value }}
{% assign collection = page.metafields.custom.collection.value %}
Accessing Reference Object : {% raw %}{% assign collection = page.metafields.custom.collection.value %}{% endraw %}
  • title :{% raw %}{{ collection.title }}{% endraw %}{{ collection.title }}
  • url :{% raw %}{{ collection.url | link_to: collection.url }}{% endraw %}{{ collection.url | link_to: collection.url }}
Metafield Type: metaobjects
Liquid Metafield Code: {% raw %}{{ page.metafields.custom.metaobjects.value }}{% endraw %}
Metafield Value: {{ page.metafields.custom.metaobjects.value }}
{% assign metaObject = page.metafields.custom.metaobjects.value %}
Accessing MetaObject : {% raw %}{% assign metaObject = page.metafields.custom.metaobjects.value %}{% endraw %}
  • name :{% raw %}{{ metaObject.name }}{% endraw %}{{ metaObject.name }}
  • gender :{% raw %}{{ metaObject.gender }}{% endraw %}{{ metaObject.gender }}
Metafield Type: file
Liquid Metafield Code: {% raw %}{{ page.metafields.custom.file.value }}{% endraw %}
Metafield Value: {{ page.metafields.custom.file.value }}
{% assign file = page.metafields.custom.file.value %}
Accessing Reference Object : {% raw %}{% assign file = page.metafields.custom.file.value %}{% endraw %}
  • file :{% raw %}{{ file | image_url: width: 100 | image_tag }}{% endraw %} {{ file | image_url: width: 100 | image_tag }}

模板渲染

Shopify二次开发之五:元字段(Metafields)_第7张图片

Shopify二次开发之五:元字段(Metafields)_第8张图片

你可能感兴趣的:(shopify,前端,shopify)