Solved

How can I display a specific metafield in the product collection?

DreamDrops
Tourist
10 0 1

Hello guys,

 

I want to display a metafield in the product collection.
However, not the correct metafield is displayed, but all of them. (See image)
How can I fix this?
You would help me alot. 

 

 

DreamDrops_0-1709987155301.png
My code: 


{% for product in collection.products %}
<div class="product-grid-item">
<div class="gerundetes-element">
<p>🧑🏽"...Riecht wie {% if product.metafields.custom.riecht_wie_ %}
<b>{{ product.metafields.custom.riecht_wie_ | metafield_text}}“</b>
{% else %}
<b>eine bekannte Luxusmarke"</b>
{% endif %}</p>
</div>
<!-- Include other product grid elements here -->
</div>
{% endfor %}


Accepted Solution (1)
PaulMartin
Shopify Partner
193 34 61

This is an accepted solution.

Try this one out, and let me know if it works:

<div class="product-grid-item">
<div class="gerundetes-element">
<p>🧑🏽"...Riecht wie {% if card_product.metafields.custom.riecht_wie_ %}
<b>{{ card_product.metafields.custom.riecht_wie_ | metafield_text}}“</b>
{% else %}
<b>eine bekannte Luxusmarke"</b>
{% endif %}</p>
</div>
<!-- Include other product grid elements here -->
</div>
{% endfor %}

View solution in original post

Replies 14 (14)

webwondersco
Shopify Partner
1036 157 154

@DreamDrops  what is the issue you are actually facing? does it show all products value in each products?

 

Web Wonders | Shopify Partner | Connect here!
- Question answered? Mark as Accepted Solution, reply helpful? Click Like
- Hire me, if you want to design, re-design, develop a store, or make changes to the pre-built store.
a small Coffee Tip would be greatly appreciated. 🙂
DreamDrops
Tourist
10 0 1

@webwondersco  you are right, it show every metafield value

PaulMartin
Shopify Partner
193 34 61

{% for product in collection.products %}

This is the code that is causing it. You're essentially calling all the products in the collection and getting the metafield values for each product and shoving all that in one product card. You need to remove that and reference the product that is called by the card.

DreamDrops
Tourist
10 0 1

@PaulMartin how can I reference it? I am very bad in programming. 

PaulMartin
Shopify Partner
193 34 61

@DreamDrops
It looks like you are not using Dawn theme. What theme are you using?

DreamDrops
Tourist
10 0 1

@PaulMartin  I use Sense

PaulMartin
Shopify Partner
193 34 61

Alright. Let me see what I can do and let you know.

PaulMartin
Shopify Partner
193 34 61

This is an accepted solution.

Try this one out, and let me know if it works:

<div class="product-grid-item">
<div class="gerundetes-element">
<p>🧑🏽"...Riecht wie {% if card_product.metafields.custom.riecht_wie_ %}
<b>{{ card_product.metafields.custom.riecht_wie_ | metafield_text}}“</b>
{% else %}
<b>eine bekannte Luxusmarke"</b>
{% endif %}</p>
</div>
<!-- Include other product grid elements here -->
</div>
{% endfor %}
DreamDrops
Tourist
10 0 1

@PaulMartin 
I had to delete {% endfor %}.
It works, thank you alot. 

PaulMartin
Shopify Partner
193 34 61

Forgot that was there, my bad. You're welcome!

DreamDrops
Tourist
10 0 1

@PaulMartin You are today my hero.

Thank you very much. 

PaulMartin
Shopify Partner
193 34 61

Hahaha, you're welcome again DreamDrops! 😊

DreamDrops
Tourist
10 0 1
PaulMartin
Shopify Partner
193 34 61

@DreamDrops wrote:

@PaulMartin 

I tried this:

<div class="product-grid-item">
<div class="gerundetes-element">
<p>🧑🏽"...Riecht wie {% if product.metafields.custom.riecht_wie_ %}
<b>{{ card_product.metafields.custom.riecht_wie_ | metafield_text}}“</b>
{% else %}
<b>eine bekannte Luxusmarke"</b>
{% endif %}</p>
</div>
<!-- Include other product grid elements here -->
</div>

you think thats good so?


It depends. The "product" is only accessible inside the product page or when it is called by something else like collection.