Pulling product metafields into email notifications

Hi all,

I am having great difficulty in pulling product metafields into email notifications. I have tried many things over the last few hours (and learnt a lot!), but I have reached the end where I believe I’m doing everything correct, yet it isn’t working.

The goal is to pull the value of a metafield from a product to display in a notification email (in particular the pick up notification email).

<table>

                                        
                                            
                                                
                                                    {% for line in line_items %}
                                                    
                                                    {% endfor %}
                                                <table>

                                                        
                                                            <table>

                                                                <tr>
<td>

                                                                    {% if line.image %}
                                                                    
                                                                    {% endif %}
                                                                

</td>

                                                                <td>

                                                                    {% if line.product.title %}
                                                                    {% assign line_title = line.product.title %}
                                                                    {% else %}
                                                                    {% assign line_title = line.title %}
                                                                    {% endif %}
                                                                    {% if line.quantity < line.quantity %}
                                                                    {% capture line_display %}
                                                                    {{ line.quantity }} of {{ line.quantity }}
                                                                    {% endcapture %}
                                                                    {% else %}
                                                                    {% assign line_display = line.quantity %}
                                                                    {% endif %}
                                                                    
                                                                        {{ line_title }} × {{ line_display }}
                                                                    
                                                                    

                                                                    {% if line.variant_title != 'Default Title' %}
                                                                    
                                                                        {{ line.variant_title }}
                                                                    
                                                                    

 Here is what I am trying to show: {{ line.product.metafield.custom.duration.value }} and {{ line.product.metafield.reviews.rating_count.value }}.
                                                                    {% endif %}
                                                                

</td>

                                                            </tr>
</table>
                                                        
                                                    </table>

I am trying to pull these two metafields: {{ line.product.metafield.custom.duration.value }} and {{ line.product.metafield.reviews.rating_count.value }}.

  • custom.duration is a single line text
  • reviews.rating_count is a integer
  • The thing I am actually trying to pull is a list of ‘single line texts’, yet I can’t even figure out the syntax to pull a single item, let alone setting up my for loop to include all the items from an array
    • The one I tried with an array was {% for req as line.product.metafield.custom.requirements %} {{ req }}, {% endfor %} where line.product.metafield.custom.requirements is a list of single line text items.

Many thanks in advance and hoping someone can assist with where I’ve gone wrong!

Additionally, I have read and tried the things from the following threads/pages but obviously doing something wrong:

https://www.shopify.com/au/partners/blog/metafields

https://shopify.dev/docs/api/liquid/objects/metafield

https://shopify.dev/docs/apps/custom-data/metafields/manage-metafields#step-2-retrieve-a-metafield

https://community.shopify.com/topic/1151643

https://community.shopify.com/post/148358

https://community.shopify.com/topic/1787934

https://community.shopify.com/topic/1151643

https://community.shopify.com/post/1605216

I have tried it again and still can’t get it to work - if anyone knows or can assist with this, would be much appreciated. Thanks!

Ok, I found how to pull the product metafields for a List of Single Text items, but believe it will work for any custom metafield.

TLDR: You need to run a for loop across the items, and have the correct syntax to pull it.

Code:

Because this will pull the custom metafield for each item in the email, then it needs to be put inside the “for” loop of “line in line_items”, because that is the list of items in the Order Confirmation email:

{% for line in line_items %}

… // a bunch of code // …

{% for req in line.product.metafields.custom.requirements %}- {{ req }}
{% endfor %}

// end code

(note that my custom metafields name is “requirements” above, but yours will be whatever you’ve called it)

The syntax is line.product.metafields.custom.(your metafield namespace) - but because it is a list (array) you need to cycle through them, that’s why it’s got the for loop and the endfor at the end.

If yours isn’t a list, and just a single value (eg an integer, single line text etc), then you won’t need the for loop, and you could just use:

{{ line.product.metafields.custom.requirements }}

Hope this helps someone!

How can I pull URL of a file that is pdf from product metafield. I want to provide a download link of the file in email template. Can you please help me out?