Liquid code for checking if item is in order

Solved

Liquid code for checking if item is in order

MrPaulRI
Visitor
1 0 1

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!

Accepted Solution (1)

goldi07
Trailblazer
181 14 19

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 😊

 

Was I helpful?

Buy me a coffee



Want to modify or custom changes or bug fix on store . Or Need help with your store? Or -Want Complete Storefront
Email me -Goldi184507@gmail.com - Skype: live:.cid.819bad8ddb52736c -Whatsapp: +919317950519
Checkout Some Free Sections Here

View solution in original post

Replies 2 (2)

Sandeep81
Shopify Partner
126 18 22

hi,
Please add
line_item.product.title instead of line_item.title

Hey if it works, please give a Like or mark it as a solution.
Please Let me Know if not solved.
Here I am: sandeepkhandagade@gmail.com
Thanks & Regards
Sandeep

goldi07
Trailblazer
181 14 19

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 😊

 

Was I helpful?

Buy me a coffee



Want to modify or custom changes or bug fix on store . Or Need help with your store? Or -Want Complete Storefront
Email me -Goldi184507@gmail.com - Skype: live:.cid.819bad8ddb52736c -Whatsapp: +919317950519
Checkout Some Free Sections Here