Conversations about creating, managing, and using metafields to store and retrieve custom data for apps and themes.
The goal is to display product metafields on the collection pages, yet calling the following lines of code inside card-product.liquid returns null/blank:
{% if product.metafields.custom.width %} <p>{{ product.metafields.custom.width }}</p> {% endif %}
{{ product.metafields | json }}
This seems to be an issue with scope. How does one enable collections to access product metafields in the Dawn theme?
Below are images of the metafields for namespace info.
Solved! Go to the solution
This is an accepted solution.
Hi @willbeing
You need to use a forloop. See example below
{% for product in collection.products %}
{% if product.metafields.custom.width.value %}
<p>{{ product.metafields.custom.width.value }}</p>
{% endif %}
{% endfor %}
Please don't forget to Like and Mark Solution to the post that helped you. Thanks!
This is an accepted solution.
No need to respond, @Made4uo-Ribe - looks like comparing product.id to card.product.id works:
{% for product in collection.products %}
{% if product.metafields.custom.width.value and product.id == card_product.id %}
<p>{{ product.metafields.custom.width.value }}</p>
{% endif %}
{% endfor %}
This is an accepted solution.
Hi @willbeing
You need to use a forloop. See example below
{% for product in collection.products %}
{% if product.metafields.custom.width.value %}
<p>{{ product.metafields.custom.width.value }}</p>
{% endif %}
{% endfor %}
Please don't forget to Like and Mark Solution to the post that helped you. Thanks!
Thank you, @Made4uo-Ribe. When used inside card-product.liquid, this lists the width of every product in the collection within each card. What condition should be added to list only the width for that card?
This is an accepted solution.
No need to respond, @Made4uo-Ribe - looks like comparing product.id to card.product.id works:
{% for product in collection.products %}
{% if product.metafields.custom.width.value and product.id == card_product.id %}
<p>{{ product.metafields.custom.width.value }}</p>
{% endif %}
{% endfor %}