Best method for modifying order confirmation email

Hello Shopify / Liquid gurus,

I am hoping one of you can help figure out the best way to modify a bit of code in my “Order Confirmation Email”.

A hand full of products I sell require VERY specific shipping and I’d like to have the text in the main body of the confirmation email include those instructions for those products; but only for orders with those products.

As best I can tell, the best way to differentiate one of the specific shipping products from the others is by using product tags as a variable. I’d like to write a small bit of code that will look at the tags of the products ordered and, if a product has one of the predetermined tags, give those instructions in the email.

For example:

If a product has tag “A”, “Shipping instructions A”

If a product has tag “B”, “Shipping instructions B”

If neither tag is associated with any product in the order, “thanks for your order”.

I have tried a million ways and none seem to work. My best guess so far looks something like this:

{% capture email_body %}
{% for tag in line.product.tags %}

{% if tag == ‘A’ %}
“Instructions for products with “Tag A””

{% elsif tag == ‘B’ %}
“Instructions for products with “Tag B””

{% else %}
“Thanks”

{% endif %}

{% endfor %}
{% endcapture %}

Thank you in advance for any help you can give me!

JRed

Can you check this logic?

{% assign foundTagA = false %}
{% assign foundTagB = false %}

{% for line in line_items %}
    {% for tag in line.product.tags %}
        {% if tag == 'A' %}
            {% assign foundTagA = true %}
        {% elsif tag == 'B' %}
            {% assign foundTagB = true %}
        {% endif %}
    {% endfor %}
{% endfor %}
  

{% if foundTagA %}
 "Instructions for products with "Tag A""
{% endif %}

{% if foundTagB %}
    "Instructions for products with "Tag B""
{% endif %}

"Thanks"
2 Likes

Thank you very much! This is much closer than any result I was getting. The only issue I see is that I don’t have an “else”.

As you wrote it, it finds the tag, and puts the appropriate response, but it also adds the “else” text as well. Given the example above:

If it has TagA: it will output “TagA instructions” and “Thanks”.

Is there a way to have something like “if no product has TagA or TagB, output Thanks!”

I cannot thank you enough. I have been fighting with this for hours.

I got it figured out. I changed the last segment to if/elsif with an “else” and it worked perfectly!

{% if foundTagA %}
 "Instructions for products with "Tag A""

{% elsif foundTagB %}
    "Instructions for products with "Tag B""

{% else %}
    "Thanks"

{% endif %}

Thanks again.

JRed