如何在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
相关产品推荐
相关产品推荐

