Re: Product Metafields in Fulfillment Mails

Solved

How can I individually list product metafields in fulfillment emails?

eric-aplanta
Tourist
8 0 4

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 %}

Accepted Solution (1)
MetafieldsGuru
Shopify Partner
160 32 103

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>

 

Check out Metafields Guru, the #1 ranked metafields app.

Bulk editor | Data import/export | Metafield sets | Browser extension

View solution in original post

Replies 8 (8)

MetafieldsGuru
Shopify Partner
160 32 103

I guess the problem is that you're trying to loop through all the products while you're already looping through the products:

1.png

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 %}
Check out Metafields Guru, the #1 ranked metafields app.

Bulk editor | Data import/export | Metafield sets | Browser extension
eric-aplanta
Tourist
8 0 4

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?

 

MetafieldsGuru
Shopify Partner
160 32 103

I tried placing a similar code snippet into the cart template of my theme (Debut) and it worked like a charm:

1.png

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 }}

 

Check out Metafields Guru, the #1 ranked metafields app.

Bulk editor | Data import/export | Metafield sets | Browser extension
eric-aplanta
Tourist
8 0 4

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.

MetafieldsGuru
Shopify Partner
160 32 103

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.

Check out Metafields Guru, the #1 ranked metafields app.

Bulk editor | Data import/export | Metafield sets | Browser extension
eric-aplanta
Tourist
8 0 4

thank you very much but I don't think I understand it right 🙂

MetafieldsGuru
Shopify Partner
160 32 103

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>

 

Check out Metafields Guru, the #1 ranked metafields app.

Bulk editor | Data import/export | Metafield sets | Browser extension
eric-aplanta
Tourist
8 0 4

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 ;))