All things Shopify and commerce
Hello, I have the following code in my confirmation email:
<!-- Handle digital products --> {% assign punches = order.line_items | map: "title" | contains: "10 Punch Pass" %} {% unless punches == empty %} <p>Text for punches.</p> {% endunless %} {% assign day_pass = order.line_items | map: "title" | contains: "Day Pass" %} {% unless day_pass == empty %} <p>Text for passes.</p> {% endunless %}
However, whether these products are in the order or not, the text always displays. Would appreciate any help fixing the issue.
Thank you!
Solved! Go to the solution
This is an accepted solution.
Hello @MrPaulRI
Your issue comes from how Liquid handles contains when used with arrays. The map filter returns an array, and contains doesn't work correctly with arrays in Liquid. Instead, you should loop through order.line_items and check each item’s title.
Corrected Code:
<!-- Handle digital products -->
{% assign punches_found = false %}
{% assign day_pass_found = false %}
{% for item in order.line_items %}
{% if item.title contains "10 Punch Pass" %}
{% assign punches_found = true %}
{% endif %}
{% if item.title contains "Day Pass" %}
{% assign day_pass_found = true %}
{% endif %}
{% endfor %}
{% if punches_found %}
<p>Text for punches.</p>
{% endif %}
{% if day_pass_found %}
<p>Text for passes.</p>
{% endif %}
Explanation of Fix:
1.Create boolean flags (punches_found and day_pass_found) to check if the products exist.
2.Loop through order.line_items and check if the item’s title contains the keywords.
3.Set the flags to true if a match is found.
4.After the loop, conditionally display the text based on whether the flags are true.
This will correctly check for "10 Punch Pass" and "Day Pass" items in the order and display the text only if they are present.
Thank you 😊
hi,
Please add
line_item.product.title instead of line_item.title
This is an accepted solution.
Hello @MrPaulRI
Your issue comes from how Liquid handles contains when used with arrays. The map filter returns an array, and contains doesn't work correctly with arrays in Liquid. Instead, you should loop through order.line_items and check each item’s title.
Corrected Code:
<!-- Handle digital products -->
{% assign punches_found = false %}
{% assign day_pass_found = false %}
{% for item in order.line_items %}
{% if item.title contains "10 Punch Pass" %}
{% assign punches_found = true %}
{% endif %}
{% if item.title contains "Day Pass" %}
{% assign day_pass_found = true %}
{% endif %}
{% endfor %}
{% if punches_found %}
<p>Text for punches.</p>
{% endif %}
{% if day_pass_found %}
<p>Text for passes.</p>
{% endif %}
Explanation of Fix:
1.Create boolean flags (punches_found and day_pass_found) to check if the products exist.
2.Loop through order.line_items and check if the item’s title contains the keywords.
3.Set the flags to true if a match is found.
4.After the loop, conditionally display the text based on whether the flags are true.
This will correctly check for "10 Punch Pass" and "Day Pass" items in the order and display the text only if they are present.
Thank you 😊
Hey Community 👋 Did you know that March 15th is National Everything You Think Is W...
By JasonH Apr 1, 2025Discover how to increase the efficiency of commerce operations with Shopify Academy's l...
By Jacqui Mar 26, 2025Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025