---
title: "metafield"

doc_type: static
doc_category: static
doc_version: v20260901
source_url:
  html: 'https://developer.shopline.com/zh-hans-cn/docs/handlebars/objects/metafield'
  md: 'https://developer.shopline.com/zh-hans-cn/docs/handlebars/objects/metafield.md'
---


# metafield

<div className="code-flex">

<div>
  <ObjectSummary>
  附加到父对象的元字段。
  </ObjectSummary>

<ObjectDescription>
  要了解如何访问特定对象上的元字段，请浏览元字段。
</ObjectDescription>

<ObjectDescription>
  元字段支持多种数据类型，这决定了存储在元字段中的信息类型。您还可以使用元字段过滤器以特定于类型的格式输出元字段内容
  。
</ObjectDescription>

<ObjectDescription>
  您不能在 handlebars 中创建元字段。元字段只能通过以下方式创建：
</ObjectDescription>

:::note{title='Note'}
<ul>
  <li>在Shopline后台</li>
  <li>通过 open api</li>
</ul>
:::

<br />
#### 参数

  <ObjectProperty
    name="list?"
    types={["boolean"]}
  >
    <ObjectSummary>
      如果元字段是 `list` 类型返回true，否则返回 `false`。
    </ObjectSummary>
  </ObjectProperty>

  <ObjectProperty
    name="type"
    types={["string"]}
  >
    <ObjectDescription>
元字段的类型。
<table><thead><tr><th>可选值</th></tr></thead><tbody><tr><td>single_line_text_field</td></tr><tr><td>multi_line_text_field</td></tr><tr><td>product_reference</td></tr><tr><td>collection_reference</td></tr><tr><td>variant_reference</td></tr><tr><td>page_reference</td></tr><tr><td>file_reference</td></tr><tr><td>number_integer</td></tr><tr><td>number_decimal</td></tr><tr><td>date</td></tr><tr><td>date_time</td></tr><tr><td>url_reference</td></tr><tr><td>json</td></tr><tr><td>boolean</td></tr><tr><td>color</td></tr><tr><td>weight</td></tr><tr><td>volume</td></tr><tr><td>dimension</td></tr><tr><td>rating</td></tr><tr><td>list</td></tr><tr><td>money</td></tr></tbody></table>
</ObjectDescription>

  </ObjectProperty>

  <ObjectProperty name="value">
    <ObjectSummary>
      元字段的值。
<br/> 下表概述了每种元字段类型的值格式：

<table>
        <thead>
            <tr>
                <th>类型</th>
                <th>返回值</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    `single_line_text_field`<br></br>
                    `multi_line_text_field`
                </td>
                <td>字符串</td>
            </tr>
            <tr>
                <td>
                    `product_reference`
                </td>
                <td>商品 Object</td>
            </tr>
            <tr>
                <td>
                    `collection_reference`
                </td>
                <td>商品分类 Object</td>
            </tr>
            <tr>
                <td>
                    `variant_reference`
                </td>
                <td>SKU Object</td>
            </tr>
            <tr>
                <td>
                    `page_reference`
                </td>
                <td>页面 Object</td>
            </tr>
            <tr>
                <td>
                    `file_reference`
                </td>
                <td>
                    媒体 Object
                </td>
            </tr>
            <tr>
                <td>
                    `number_integer`<br></br>
                    `number_decimal`
                </td>
                <td>数字</td>
            </tr>
            <tr>
                <td>
                    `date`<br></br>
                    `date_time`
                </td>
                <td>日期</td>
            </tr>
            <tr>
                <td>
                    `url_reference`
                </td>
                <td>url 字符串</td>
            </tr>
            <tr>
                <td>
                    `json`
                </td>
                <td>JSON object</td>
            </tr>
            <tr>
                <td>
                    `boolean`
                </td>
                <td>布尔值</td>
            </tr>
            <tr>
                <td>
                    `color`
                </td>
                <td>颜色 Object</td>
            </tr>
            <tr>
                <td>
                    `weight`<br></br>
                    `volume`<br></br>
                    `dimension`
                </td>
                <td>度量 Object</td>
            </tr>
            <tr>
                <td>
                    `rating`
                </td>
                <td>评分 Object</td>
            </tr>
            <tr>
                <td>
                    `list`
                </td>
                <td>值列表</td>
            </tr>
            <tr>
                <td>
                    `money`
                </td>
                <td>货币 Object</td>
            </tr>
        </tbody>
    </table>
       </ObjectSummary>

    <ObjectDescription>

    </ObjectDescription>

  </ObjectProperty>
</div>

<div>
  <ObjectAccess
    object="metafields"
    global={false}
    parents={[
      {object:"article.metafields",link:"/docsv2/ec20/hdwvcvrgnfcjf4gq/96arp92pnm7h8rk3#metafields"},
      {object:"blog.metafields",link:"/docsv2/ec20/hdwvcvrgnfcjf4gq/ysxb57s3p8z99vpc#metafields"},
      {object:"collection.metafields",link:"/docsv2/ec20/hdwvcvrgnfcjf4gq/bgaerupeyfawz4am#metafields"},
      {object:"customer.metafields",link:"/docsv2/ec20/hdwvcvrgnfcjf4gq/s3xh1gqr1mjztqdf#metafields"},
      {object:"order.metafields",link:"/docsv2/ec20/hdwvcvrgnfcjf4gq/p3qwtbcffbycybwg#metafields"},
      {object:"page.metafields",link:"/docsv2/ec20/hdwvcvrgnfcjf4gq/bcwatgnw6x191p2f#metafields"},
      {object:"product.metafields",link:"/docsv2/ec20/hdwvcvrgnfcjf4gq/tfzgv1cp3z3wzk1s#metafields"},
      {object:"shop.metafields",link:"/docsv2/ec20/hdwvcvrgnfcjf4gq/vwapf2hmu9mbjzxp#metafields"},
      {object:"variant.metafields",link:"/docsv2/ec20/hdwvcvrgnfcjf4gq/gjjjhvusthxd5ucy#metafields"},
    ]}
  />
</div>
</div>

<br />
<br />
<div>
  <ObjectDescription>访问元字段</ObjectDescription>
  <ObjectSummary>
    元字段的访问路径由两层组成：
    <ul>
      <li>命名空间 - 一组元字段以防止冲突。</li>
      <li>key - 元字段名称。</li>
    </ul>
    鉴于此，您可以使用以下语法访问元字段对象：
  </ObjectSummary>
</div>

```handlebars
{{ resource.metafields.namespace.key }}
```

<Example data={['product.metafields.demo_namespace.demo_key']}>
  ```handlebars
  Type: {{ product.metafields.demo_namespace.demo_key.type }}
  Value: {{ product.metafields.demo_namespace.demo_key.value }}
  ```
</Example>

<div>
  <ObjectDescription>在元字段表达式中使用变量</ObjectDescription>
</div>

<Example data={['product.metafields.demo_namespace.demo_key']}>
  ```handlebars
  {{ assign 'key' 'demo_key' }}
  {{ assign 'demoMetafield' (get key product.metafields.demo_namespace) }}

  Type: {{ demoMetafield.type }}
  Value: {{ demoMetafield.value }}
  ```
</Example>

<div>
  <ObjectDescription>访问 `json` 类型的元字段</ObjectDescription>
  <ObjectSummary>
    `json` 类型元字段的 `value` 属性会返回一个JSON对象。
  </ObjectSummary>
</div>

<Example data={['product.metafields.info.weight']}>
  ```handlebars
  Weight: {{ product.metafields.info.weight.value.numeric }}

  {{#each product.metafields.info.weight.value as |property|}}
    {{capitalize @key}}: {{property}}
  {{/each}}
  ```
</Example>

<div>
  <ObjectDescription>访问 `list` 类型的元字段</ObjectDescription>
  <ObjectSummary>
    `list` 类型元字段的 `value` 属性会返回一个数组。您可以遍历数组以访问值。
  </ObjectSummary>
</div>

<Example data={['product.metafields.info.multiple']}>
  ```handlebars
  Type: {{ product.metafields.info.multiple.type }}

  {{#each product.metafields.info.multiple.value as |item|}}
    {{ item.product.title }}
  {{/each}}
  ```
</Example>

<ObjectSummary>
  如果 `list` 是 `single_line_text_field` 类型，您也可以通过索引直接访问数组中的项目。
</ObjectSummary>
  
<Example data={['product.metafields.info.descriptions']}>
  ```handlebars
  Type: {{ product.metafields.info.descriptions.type }}

  First description in list: {{ product.metafields.info.descriptions.value.[0] }}
  Last description in list: {{ last product.metafields.info.descriptions.value }}
  ```
</Example>

<div>
  <ObjectDescription>OS2.0及以下版本主题注意事项：</ObjectDescription>
  <ObjectSummary>
    由于实现机制的不同，在OS2.0及以下主题使用元字段会有以下限制：
    - 仅支持单个值类型的元字段，暂不支持 list 多个值类型
    - 暂不支持参考类型的元字段
    - `metafields` 前的资源对象必须为固定的字段名，不可自定义

        |资源|字段|
        |---|---|
        |商品|product.metafields|
        |商品分类|collection.metafields|
        |商品款式|variant.metafields|
        |页面|page.metafields|
        |客户|customer.metafields|
        |博客|article.metafields|
        |博客集合|blog.metafields|
        |订单|order.metafields|
      
      ```handlebars
      {{!-- 正确 --}}
      {{ product.metafields.information.pickup_locations.value }}

      {{!-- 错误 --}}
      {{ assign 'p' product }}
      {{ p.metafields.information.pickup_locations.value }}

      {{!-- 正确 --}}
      {{#each collection.products as |product|}}
        {{ product.metafields.information.pickup_locations.value }}
      {{/each}}

      {{!-- 错误 --}}
      {{#each collection.products as |p|}}
        {{ p.metafields.information.pickup_locations.value }}
      {{/each}}
      ```
    - 取值表达式的格式必须满足 `${ownerResource}.metafields.${namespace}.${key}.(value|type)`
      
      ```handlebars
      {{!-- 错误 --}}
      {{ assign 'mf' product.metafields }}
      {{ mf.information.pickup_locations.value }}

      {{!-- 错误 --}}
      {{ assign 'ns' product.metafields.information }}
      {{ ns.pickup_locations.value }}
      ```
  </ObjectSummary>
</div>