英文:
Display collection metafield in product page (Shopify)
问题
有一种方法可以在产品页面中显示集合元字段吗?
每个产品都有一个与集合匹配的产品类型,因此在该产品的产品页面上,我想显示该产品的元字段。
{{ collection.metafields.custom.metafield1 }} 不起作用,因为当然我不在集合页面,所以我需要链接collection.metafields.custom.metafield1和product.type,但我不知道如何做。
希望我表达清楚,谢谢。
每个产品都有一个与集合匹配的产品类型,因此在该产品的产品页面上,我想显示该产品的元字段。
英文:
there is a way to display a collection metafield in the porduct page?
Each product have a product type that matches the collection, so in the product page of this product I want to display the metafield of that product.
{{ collection.metafields.custom.metafield1 }} doesn't work because of course I'm not in the collection page, so I need to link collection.metafields.custom.metafield1 and product.type, but I don't know how.
Hope I was clear, thank you.
Each product have a product type that matches the collection, so in the product page of this product I want to display the metafield of that product.
答案1
得分: 1
我用以下代码解决了:
{% assign collectionHandle = product.type %}
{% assign the_collection = collections[collectionHandle] %}
{% assign my_metafield = the_collection.metafields.custom.categoria_1 %}
{{ my_metafield.value }}
如你建议的,解决这个问题很有趣
英文:
I solved with the following code
{% assign collectionHandle = product.type %}
{% assign the_collection = collections[collectionHandle] %}
{% assign my_metafield = the_collection.metafields.custom.categoria_1 %}
{{ my_metafield.value }}
As you suggest it was fun to solve the problem
答案2
得分: 0
在Liquid中,每个产品都有一个类型。因此,您可以在集合对象上使用它,以获取您想要的集合,以及分配的特殊元字段。
{% assign the_collection = collections['{{ product.type }}'] %}
这将根据当前产品类型为您的变量分配某个集合,假设它存在。现在,您可以请求该集合的元字段,该元字段可能存在,也可能不存在。
{% assign my_metafield = the_collection.metafields.some_namespace.some_key %}
有了这个,您可以获取值并随心所欲地展示出来。
<h1>{{ my_metafield.value }}</h1>
英文:
Hard to understand your question, but it would probably go like this. In Liquid, every product has a type. So you would use that on the collections object to get just the collection you want, and from that, the special metafield assigned.
{% assign the_collection = collections['{{ product.type }}'] %}
That assigns some collection, assuming it exists, based on the current product type, to your variable. Now you would ask for your special metafield that may or may not exist in that collection's metafields.
{% assign my_metafield = the_collection.metafields.some_namespace.some_key %}
And with that, you could get the value and splash it out there as you please.
<h1> {{ my_metafield.value }} </h1>
答案3
得分: 0
是的,我理解您的观点,我理解了您的代码并尝试使用它:
{% assign the_collection = collections['{{ product.type }}'] %}
{% assign my_metafield = the_collection.metafields.custom.categoria_1 %}
<span>{{ my_metafield.value }}</span>
我不明白为什么它不起作用。
- 产品类型存在且为"Piatti"。
- 集合"Piatti"存在并具有自定义的 categoria_1 元字段。
我附上了元字段的截图,以便更清楚理解。
英文:
yes I understand your point, and I figure out your code and I tried to use it:
{% assign the_collection = collections['{{ product.type }}'] %}
{% assign my_metafield = the_collection.metafields.custom.categoria_1 %}
<span>{{ my_metafield.value }}</span>
and I don't understand why it does not work.
- The product type exists and is "Piatti"
- The collection "Piatti" exists and has the metafield custom categoria_1
I attach a sceenshot of the metafield in order to be more cleare.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论