数据类型
元字段 通过数据类型规定可存储的内容,并为每种数据类型提供内置格式校验。无论使用何种数据类型,元字段的值均以字符串形式传入和存储。在主题模板中,你可以通过 metafield object 访问元字段。
基础类型
基础类型元字段存储文本、数字、日期和带单位的度量值等信息。
| 类型 | 描述 | 值类型 |
|---|---|---|
boolean | 布尔值(true 或 false)。 | boolean |
color | 颜色的十六进制代码。 | string |
date | ISO 8601 格式的日期,不含时区假设。 | string |
date_time | ISO 8601 格式的日期和时间,不含时区假设,默认使用格林威治标准时间(GMT)。 | string |
dimension | 长度的数值和单位。 有效单位:
| JSON 对象 |
json | JSON 可序列化的值,可以是对象、数组、字符串、数字、布尔值或 null。 | JSON 对象 |
link | 文本与 URL 的组合,用于存储链接内容。 | JSON 对象 |
money | 数值金额,附带与店铺货币匹配的货币代码。 | JSON 对象 |
multi_line_text_field | 多行文本字段,最大 65 KB。 | string |
number_decimal | 小数,范围为 ±9,999,999,999,999.999999999。 | string |
number_integer | 整数,范围为 ±9,999,999,999,999。 | integer |
rating | 在指定范围内的评分,需要提供 scale_min 和 scale_max 校验参数。 | JSON 对象 |
rich_text_field | 支持标题、列表、链接、加粗和斜体的 富文本字段。 | JSON 对象 |
single_line_text_field | 单行文本字段,最大 65 KB。 | string |
url | URL,支持以下协议:https、http、mailto、sms、tel。 | string |
volume | 体积的数值和单位。 有效单位:
| JSON 对象 |
weight | 重量的数值和单位。 有效单位:
| JSON 对象 |
基础类型代码示例
以下示例演示每种基础类型的预期值格式:
| 类型 | 示例 |
|---|---|
boolean | |
color | |
date | |
date_time | |
dimension | |
json | |
link | |
money | |
multi_line_text_field | |
number_decimal | |
number_integer | |
rating | |
rich_text_field | |
single_line_text_field | |
url | |
volume | |
weight | |
引用类型
引用类型元字段用于存储对店铺资源的引用。
| 类型 | 描述 | 值类型 |
|---|---|---|
collection_reference | 对商品分类的引用。 | string |
file_reference | 对文件的引用。 | string |
metaobject_reference | 对元对象条目的引用。 | string |
page_reference | 对页面的引用。 | string |
product_reference | 对商品的引用。 | string |
variant_reference | 对商品款式的引用。 | string |
article_reference | 对博客文章的引用。 | string |
引用类型代码示例
| 类型 | 示例 |
|---|---|
collection_reference | |
file_reference | |
metaobject_reference | |
page_reference | |
product_reference | |
variant_reference | |
article_reference | |
列表类型
列表类型元字段用于在单个元字段中存储多个值。值以 JSON 数组格式表示,每个列表类型元字段最多可包含 128 个元素。
| 类型 | 描述 |
|---|---|
list.color | 十六进制颜色代码的列表。 |
list.collection_reference | 商品分类引用的列表。 |
list.date | ISO 8601 格式日期的列表。 |
list.date_time | ISO 8601 格式日期时间的列表。 |
list.dimension | 长度的数值和单位的列表。 有效单位:
|
list.file_reference | 文件引用的列表。 |
list.link | 文本与 URL 组合的列表,用于存储一组链接。 |
list.metaobject_reference | 元对象条目引用的列表。 |
list.number_decimal | 小数的列表。 |
list.number_integer | 整数的列表。 |
list.page_reference | 页面引用的列表。 |
list.article_reference | 博客文章引用的列表。 |
list.product_reference | 商品引用的列表。 |
list.rating | 在指定范围内的评分列表。 |
list.single_line_text_field | 单行文本字段的列表。 |
list.url | URL 的列表,支持 https、http、mailto、sms、tel 协议。 |
list.variant_reference | 商品款式引用的列表。 |
list.volume | 体积的数值和单位的列表。 有效单位:
|
list.weight | 重量的数值和单位的列表。 有效单位:
|
列表类型代码示例
| 类型 | 示例 |
|---|---|
list.color | |
list.collection_reference | |
list.date | |
list.date_time | |
list.dimension | |
list.file_reference | |
list.link | |
list.metaobject_reference | |
list.number_decimal | |
list.number_integer | |
list.page_reference | |
list.article_reference | |
list.product_reference | |
list.rating | |
list.single_line_text_field | |
list.url | |
list.variant_reference | |
list.volume | |
list.weight | |
富文本格式说明
rich_text_field 类型接受具有以下结构的 JSON 对象:
{
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "This is regular text.",
"bold": true,
"italic": true
},
{
"type": "link",
"url": "https://example.com",
"title": "Example link",
"children": [
{
"type": "text",
"value": "Learn more",
"bold": true
}
]
}
]
}
]
}
加粗与斜体
文本节点可通过 bold 和 italic 设置文本样式,字段说明如下:
| 字段 | 是否必填 | 说明 |
|---|---|---|
type | 是 | 固定为 text。 |
value | 是 | 文本节点的具体内容。 |
bold | 否 | 是否加粗文本,默认值为 false。 |
italic | 否 | 是否斜体文本,默认值为 false。 |
示例
{
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "This text is bold and italic.",
"bold": true,
"italic": true
}
]
}
]
}
标题
标题节点的字段说明如下:
| 字段 | 是否必填 | 说明 |
|---|---|---|
type | 是 | 固定为 heading。 |
level | 是 | 标题级别,支持 1 到 6。 |
children | 是 | text 节点数组。 |
示例
{
"type": "root",
"children": [
{
"type": "heading",
"level": 2,
"children": [
{
"type": "text",
"value": "This is a level 2 heading"
}
]
}
]
}
超链接
超链接节点的字段说明如下:
| 字段 | 是否必填 | 说明 |
|---|---|---|
type | 是 | 固定为 link。 |
url | 是 | 链接跳转地址。 |
title | 否 | 链接标题,可用于无障碍和 SEO。 |
target | 否 | 链接打开方式,例如 _blank 表示在新标签页打开。 |
children | 是 | text 节点数组。 |
示例
{
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "link",
"url": "https://example.com",
"title": "Link to example.com",
"target": "_blank",
"children": [
{
"type": "text",
"value": "View details"
}
]
}
]
}
]
}
列表
列表节点和列表项节点的字段说明如下。
列表节点:
| 字段 | 是否必填 | 说明 |
|---|---|---|
type | 是 | 固定为 list。 |
listType | 是 | 列表类型,支持 ordered 或 unordered。 |
children | 是 | 固定为 list-item 类型的节点数组。 |
列表项节点:
| 字段 | 是否必填 | 说明 |
|---|---|---|
type | 是 | 固定为 list-item。 |
children | 是 | text 节点数组。 |
示例
{
"type": "root",
"children": [
{
"type": "list",
"listType": "unordered",
"children": [
{
"type": "list-item",
"children": [
{
"type": "text",
"value": "First item"
}
]
},
{
"type": "list-item",
"children": [
{
"type": "text",
"value": "Second item"
}
]
}
]
}
]
}
这篇文章对你有帮助吗?