You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Shopify的Liquid文件中获取元字段的全部选项

获取Shopify元字段的所有选项值

要获取自定义元字段的全部选项,你需要访问对应的**元字段定义(Metafield Definition)**对象。以下是具体的Liquid代码实现:

{% assign target_def = metafield_definitions | where: 'namespace', 'custom' | where: 'key', 'style_reference' | first %}

{% if target_def %}
  {% for option in target_def.options %}
    - {{ option.label }} (值: {{ option.value }})
  {% endfor %}
{% else %}
  <p>未找到对应的元字段定义</p>
{% endif %}

代码说明:

  • metafield_definitions是Shopify提供的全局对象,包含所有已创建的元字段定义。
  • 通过where过滤器筛选出命名空间(namespace)为custom、键(key)为style_reference的元字段定义。
  • 找到目标定义后,遍历其options属性即可获取所有选项的标签(label)和对应值(value)。

如果你的元字段实际键名是material(而非style_reference),只需将代码中的style_reference替换为material即可。

内容的提问来源于stack exchange,提问作者Divesh Choudhary

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 18:33:10