Return value if metafield not present. Metafield only exists on some order.lines

Solved

Return value if metafield not present. Metafield only exists on some order.lines

Donzel
Excursionist
31 2 32

Hi,

 

I make use of product tags and metafields within products at a product variant level.

 

I am trying to create a JSON output within 'send email' for a multiple line order where the products have many tags and specific product.variant metafields. I understand that JSON can be created in a roundabout way but this isn't the problem.

 

Trigger is when an order is created (testing as a draft order) 

Within the email are conditions in liquid and it isolates lines with a particular tags_item for one value I need to return and it will also look for a particular metafield_item value based on a namespace.

It partially works.

The test draft order I am using has 3 different order.lines/products.

 

2 of these draft order lines have the metafield in question and these output properly but the third item doesn’t have this metafield and I suspect is returning nil amounts which means it isn’t outputting at all. 

 

I’d prefer it to still show in the JSON, even if the metafield outputs as zero. The added issue is that any condition I try to create to show the line without a metafield doesn’t show the metafield but also returns the line items that DO have metafields a second time, which I do not want.

 

{
"email": {{draftOrder.email | json}},
"order": {{draftOrder.name | json}},
"order_lines": [
{% for lineItems_item in draftOrder.lineItems %}
{% for tags_item in lineItems_item.product.tags %}
{% for metafields_item in lineItems_item.variant.metafields %}
{% if tags_item contains 'Filter=estimated-delivery' and metafields_item.namespace == "pre_order"%}
{"sku": {{lineItems_item.sku | json}},
"preorder": {{metafields_item.value | split: "=" | last | json}},
"timescale": {{tags_item | remove: "Filter=estimated-delivery/" | json}}
{% elsif metafields_item == nil %}
,"sku": {{lineItems_item.sku | json}},
"preorder": "0",
"timescale": {{tags_item | remove: "Filter=estimated-delivery/" | json}}
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
}
]
}
}

 

*edit* I should add that  I have tried multiple versions of this code within the body of the email to get this to work but I cannot get the line that has the tag but doesn't have the metafield to show at all.

Accepted Solution (1)

paul_n
Shopify Staff
1366 153 321

This is an accepted solution.

There are at least a couple of things in your code that could behave unpredictably:

1. You are checking the metafield namespace but not the key. You may match multiple metafields.

2. You are nesting loops with metafields and tags but probably only want to output something once. I would look to remove that tags loop if possible (I think tags can also be treated like a string, so you may just be able to search that string instead of loop). 

 

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.

View solution in original post

Replies 3 (3)

G-L
Shopify Staff (Retired)
29 4 10

Hi Donzel, this looks really like a Liquid question, I wonder if asking in the Liquid section of Technical Q&A forum would be more likely to get you help. 

 

To learn more visit the Shopify Help Center or the Community Blog.

paul_n
Shopify Staff
1366 153 321

This is an accepted solution.

There are at least a couple of things in your code that could behave unpredictably:

1. You are checking the metafield namespace but not the key. You may match multiple metafields.

2. You are nesting loops with metafields and tags but probably only want to output something once. I would look to remove that tags loop if possible (I think tags can also be treated like a string, so you may just be able to search that string instead of loop). 

 

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
Donzel
Excursionist
31 2 32

Thanks for the replies. I did end up having to break this down into two separate Flows with both outputting JSON by email to two separate automations on Power Automate (MS Flow). The first with the pre-order dates and customer details would import as a JSON array and then add to a Sharepoint list. The second would delay for ten minutes and import as a JSON array and would look for the same lines in the same Sharepoint list then update that line or create a new line if required. A further automation schedules for every day after that to look at that Sharepoint list for a date matching the notification date and performs an action based on that.