Shopify themes, liquid, logos, and UX
Hi!
I want to place Product Metafields in the Fulfillment Mail.
It works partially. but it puts me the meta field of ALL ARTICLES of the order in one row.
I want to list them individually per item in the order.
What am I doing wrong? Thank you very much.
<p><strong>Abzuwickelnde Artikel:</strong></p>
{% for line in fulfillment.fulfillment_line_items %}
<p>Artikelbezeichnung: {{ line.line_item.title }}</p>
<p>Artikelnummer: {{ line.line_item.sku }}</p>
<p>Anzahl: {{ line.quantity }}</p>
{% for line in line_items %}
{{ line.product.metafields.my_fields.hersteller_bezeichnung }}
{% endfor %}
{% endfor %}
Solved! Go to the solution
This is an accepted solution.
You're working with two different objects:
1) the "parent" for loop - the fulfillment object;
2) the "secondary" for loop - the line_item object;
In both cases, you can access the product object to work with its metafields. And that's exactly what you did in the secondary loop (the initial version that would print the metafield value for all the products at the same time).
Now you need to access the product object for both fulfillment and line_item and compare their IDs prior to displaying the metafield content.
As the result, your if statement should have a structure like this:
{% if line_secondary_loop.product.id == line.product.id %}
Also, you can try accessing the metafield directly from the line_item object, thus eliminating the need for the secondary loop (it's good for your website's performance):
<p>hersteller_bezeichnung: {{ line.line_item.product.metafields.my_fields.hersteller_bezeichnung }}</p>
I guess the problem is that you're trying to loop through all the products while you're already looping through the products:
As the result, your code is pulling the metafield for all the line items. You may want to try replacing the second for loop with a single line of code just like you did for "Artikelnummer" or "Anzahl":
<p>hersteller_bezeichnung: {{ line.line_item.metafields.my_fields.hersteller_bezeichnung }}</p>
Alternatively, if you'd like to keep that second for loop, you may want to add an if statement to compare the ID of the line item iterated by that secondary loop with the ID of the line item iterated by the parent loop and display the data only if the IDs are the same:
{% for line_secondary_loop in line_items %}
{% if line_secondary_loop.id == line.id %}
{{ line_secondary_loop.product.metafields.my_fields.hersteller_bezeichnung }}
{% endif %}
{% endfor %}
Thank you!
but unfortunately this did not lead to the desired result. the output remains empty (for both variants).
I also tried v1 first, but it always remained empty.
it looks like the fields in the "fulfilment items" are not included? can that be?
I tried placing a similar code snippet into the cart template of my theme (Debut) and it worked like a charm:
So I accessing the product metafields from the line item doesn't seem to be an issue.
I noticed that you're using an updated version of the Liquid code. Assuming that your "hersteller_bezeichnung" metafield is an OS 2.0 metafield, this part of the secondary loop:
{{ line_secondary_loop.product.metafields.my_fields.hersteller_bezeichnung }}
with this code:
{{ line_secondary_loop.product.metafields.my_fields.hersteller_bezeichnung.value }}
thanks again. yes, they are probably 2.0 meta fields. it also works wonderfully in a page template. but not in the fulfilment mail. 😞 Still empty output.
Then the problem might come from this part:
{% if line_secondary_loop.id == line.id %}
The thing is that I didn't mean to compare these exact IDs. It was more like a dummy code to demonstrate the logic. The idea was to retrieve the product objects from both line_secondary_loop and line and then compare their IDs.
thank you very much but I don't think I understand it right 🙂
This is an accepted solution.
You're working with two different objects:
1) the "parent" for loop - the fulfillment object;
2) the "secondary" for loop - the line_item object;
In both cases, you can access the product object to work with its metafields. And that's exactly what you did in the secondary loop (the initial version that would print the metafield value for all the products at the same time).
Now you need to access the product object for both fulfillment and line_item and compare their IDs prior to displaying the metafield content.
As the result, your if statement should have a structure like this:
{% if line_secondary_loop.product.id == line.product.id %}
Also, you can try accessing the metafield directly from the line_item object, thus eliminating the need for the secondary loop (it's good for your website's performance):
<p>hersteller_bezeichnung: {{ line.line_item.product.metafields.my_fields.hersteller_bezeichnung }}</p>
ok mad! now the 2nd variant works. thank you very much. (would not have thought that I am so well helped here in the forum ;))
2m ago Learn the essential skills to navigate the Shopify admin with confidence. T...
By Shopify Feb 12, 2025Learn how to expand your operations internationally with Shopify Academy’s learning path...
By Shopify Feb 4, 2025Hey Community, happy February! Looking back to January, we kicked off the year with 8....
By JasonH Feb 3, 2025