自定义产品元字段上的'size'返回'0'



我正在构建一个组件,用于检查自定义产品元字段的字符数;如果超过24个字符,则将数据作为手风琴输出,否则将完整打印元字段内容。下面的条件失败,因为元字段大小总是返回0,但我可以通过else语句看到内容打印,所以我确信路径是有效的:

{% if product.metafields.custom.product_note.size >= 24 %}
<div class="product-note has--dropdown">
<span class="item__heading item__trigger">Product information</span>
<div class="item__content">
{{ product.metafields.custom.product_note }}
</div>
</div>
{% else %}
<div class="product-note">
<div class="item__content">
{{ product.metafields.custom.product_note }}
</div>
</div>
{% endif %}

我不确定它是否相关,但product_note元字段是一个多行文本字段。如果有人能为我指出size未能产生价值的正确方向,我将不胜感激。

您可以尝试处理元字段中包含的。通过只引用命名空间和键的组合,而没有实际说明:存储在该命名空间和键处的值的长度是多少,这似乎是一种捷径。只是一个想法。你至少可以试试。

最终答案,由@David Lazar建议提供:

{% assign data_product_note = product.metafields.custom.product_note.value %}
{% if data_product_note.size >= 24 %}
<div class="product-note has--dropdown">
<span class="item__heading item__trigger">Product information</span>
<div class="item__content">
{{ data_product_note }}
</div>
</div>
{% else %}
<div class="product-note">
<div class="item__content">
{{ data_product_note }}
</div>
</div>
{% endif %}

相关内容

最新更新