Customize My Order Notification

Shopify Partner
10 0 1

I'm trying to setup an If/Else statement that allows me to input a custom message into the Order Notification Email based on what the customer has purchased. For example, if someone purchases Product 1 or Product 2, we want the Order Confirmation to add a message about that. If they decide to buy other products, we want one single message to show up for that. What happens right now is, if you add multiple products to cart - then you get multiple messages that are duplicates (IE add Product 1/2 and Products 3/4 to cart and purchase - then the order confirmation has 4 custom messages). Here is the code I'm working with:

{% for line in line_items %}
<!-- DEBUG looking at {{ line.title }} -->
{% if line.title == 'Product 1' || line.title == 'Product 2' %}
<p>Message 1 Goes Into Order Notification, only once.</p>
<hr></hr>
{% elsif line.title == 'Product 3' || line.title == 'Product 4' %}
<p>Message 2 Goes Into Order Notification, only once.</p>
<hr></hr>
{% else %}
<p>Default Message here, only once, for all other products purchased.</p>
<hr></hr>
{% endif %}
{% endfor %}

0 Likes
Highlighted
Shopify Staff (Retired)
Shopify Staff (Retired)
298 0 154

Hey Nik!

Here's what I'm thinking:

{% for line in line_items %}
<!-- DEBUG looking at {{ line.title }} -->
{% if variant.title == 'Product-1' or 'Product-2' %}
<p>Message 1 Goes Into Order Notification, only once.</p>
<hr></hr>
{% elsif variant.title == 'Product 3' or  'Product 4' %}
<p>Message 2 Goes Into Order Notification, only once.</p>
<hr></hr>
{% else %}
<p>Default Message here, only once, for all other products purchased.</p>
<hr></hr>
{% endif %}
{% endfor %}

0 Likes
Shopify Partner
10 0 1

That doesn't seem to work.. I can easily get Message 1 to work for those products, but couldn't get the "elsif" to render.. i even tried it based on variant.sku.. The problem i still have is if i take products 5 and 6 and purchase them with 1 or 2, then the default message shows up twice on the Order Confirmation page.. see below for latest rendition of the code i'm using:

 

{% for line in line_items %}
<!-- DEBUG looking at {{ line.title }} -->
{% if variant.sku == '2559021699' or '2559021635' %}
<p>Message 1 Goes Into Order Notification, only once.</p>
<hr></hr>
{% elsif variant.sku == '2559097667' or '2559097731' %}
<p>Message 2 Goes Into Order Notification, only once.</p>
<hr></hr>
{% else %}
<p>Default Message here, only once, for all other products purchased.</p>
<hr></hr>
{% endif %}
{% endfor %}

 

0 Likes
Shopify Partner
10 0 1

line.title has been the best option for me getting this to work in rendering the If or Elsif statement, for the most part.. || vs "or" in the statement has given me mixed results....

However, I'm still having the issue that if any other product is added to the order, multiple Default messages are added via else statement since it checks every line item.

 

0 Likes
Astronaut
1925 0 379

I'm a bit confused about what exactly you want your results to be.  If a customer orders Product 1 and Product 4, do you want to display (a) Message 1, (b) Message 2, or (c) Message 1 and Message 2?  I understand that you don't want any duplicates of Message 1 or duplicates of Message 2.

Assuming that you want to only display (a) Message 1, you can try the following.

<!-- Create an array of the product handles of all of the line_items.             -->
<!-- I chose to use the product handles because they are unique to each product.  -->
<!-- You can choose to arrange by product title, SKU, etc instead.                -->

{% assign delimiter = "|" %}
{% capture line_item_array %}
  {% for line in line_items %}
    {{ line.product.handle }}{{ delimiter }}
  {% endfor %}
{% endcapture %}
{% assign line_item_array = line_item_array | strip_newlines | split:delimiter %}

<!-- If array contains any of the product handles, display respective message.    -->
<!-- If not, display default message.                                             -->

{% if line_item_array contains "PRODUCT1HANDLE" or line_item_array contains "PRODUCT2HANDLE" %}
  <p>Message 1 Goes Into Order Notification, only once.</p>
{% elsif line_item_array contains "PRODUCT3HANDLE" or line_item_array contains "PRODUCT4HANDLE" %}
  <p>Message 2 Goes Into Order Notification, only once.</p>
{% else %}
  <p>Default Message here, only once, for all other products purchased.</p>
{% endif %}
<hr/>

Pay attention to the way that the if-statement condition is constructed.

I hope this helps.

0 Likes
Shopify Partner
10 0 1

Thanks. To clarify further, here is what I am wanting to do. I see what you meen by the IF statement:

1. We have membership products (total of 4 SKUs, or 2 parent products/handles)

2. We have courses products (total of "x" SKUs, or "x" parent products/handles)

3. We have all other products that can be purchased.

I am wanting to trigger a custom reponse to be added to the Order Confirmation Email based on which of these the customer purchases. It is possible for someone to purchase all 3, but more than likely someone would purchase only one of the 3. Some scenarios could be the 1st and 3rd, or the 2nd and 3rd - meaning the IF and ELSE statements would apply or the ELSIF and ELSE statement would apply.

I want to make sure that if someone purchases the 3rd option, the custom message will get triggered only once in the Order Confirmation and not once per item purchased. It looks like your recommendation has fixed that problem, thank you!

However, I'm trying to understand, based on what you are saying above (PRODUCT1HANDLE) it looks like i could simply take the product name (parent name) and remove the spaces to create this (IE - product name of Membership One would become MembershipOneHANDLE)? Or how would I trigger that line_item_array to work?

0 Likes
Astronaut
1925 0 379

The code that I provided in my previous post will display exactly one message, even if the customer purchased a "course", a "membership" and an "other" product.  I don't think that is what you're looking for.

By the way, the product handle can be found in the URL of the product page.  Alternatively, it can be found in the admin page.  Refer to this image.

 

It seems like you already have your products grouped in a certain way.  Perhaps you have them grouped in collections?  If that's true, you can try the following instead.  (Feel free to delete the comments.)

<!-- Define the collection handles that you want to check for.                                          -->
<!-- To find the handle, go to the collection page. The last part of the URL is the collection handle.  -->
<!-- Example:  URL = "www.shop.com/collections/computer-accessories/"  Handle = "computer-accessories"  -->
{% assign collection_1_handle = 'COURSES_HANDLE' %}
{% assign collection_2_handle = 'MEMBERSHIP_HANDLE' %}

<!-- Define your messages here.                                                                         -->
{% assign collection_1_message = 'A course product has been purchased.' %}
{% assign collection_2_message = 'A membership product has been purchased.' %}
{% assign collection_0_message = 'A non-course, non-membership product has been purchased.' %}

<!-- Initialize.                                                                                        -->
{% assign has_collection_1 = false %}
{% assign has_collection_2 = false %}
{% assign has_collection_0 = false %}
{% assign delimiter = "|" %}

<!-- Loop through all line_items.                                                                       -->
{% for line in line_items %}

  <!-- Compile array of collections that the line_item is in.                                          -->
  {% capture collection_handle_array %}
    {% for collection in line.product.collections %}
      {{ collection.handle }}{{ delimiter }}
    {% endfor %}
  {% endcapture %}
  {% assign collection_handle_array = collection_handle_array | strip_newlines | replace:' ','' | split:delimiter %}

  <!-- Check if the line_item falls in either collection_1 or collection_2, or neither.                 -->
  {% if collection_handle_array contains collection_1_handle %}
    {% assign has_collection_1 = true %}
  {% endif %}
  {% if collection_handle_array contains collection_2_handle %}
    {% assign has_collection_2 = true %}
  {% endif %}
  {% unless collection_handle_array contains collection_1_handle or collection_handle_array contains collection_2_handle %}
    {% assign has_collection_0 = true %}
  {% endunless %}

{% endfor %}

<!-- Print message(s).                                                                                  -->
{% if has_collection_1 %}
  {{ collection_1_message }}
{% endif %}
{% if has_collection_2 %}
  {{ collection_2_message }}
{% endif %}
{% if has_collection_0 %}
  {{ collection_0_message }}
{% endif %}

 

If you don't have the products grouped in collections, how do you group these products?  Using product tags?  Using product types?

0 Likes
Shopify Partner
10 0 1

Thanks, I will play with this and see what comes out of it.

The first two groups of products belong to a particular collection. For ELSE (or collection_0), its all other products outside of the two collections.

I think that what you provided may do the trick for me. I'll let you know.

Thank you!

 

0 Likes
Shopify Partner
10 0 1

Alex, I have played with the last bit of code you recommended based on collection names to no success. Could it be that I'm placing the code ABOVE the actual product list in the order confirmation or that our URL structure is collections/products/product-name preventing it from catching the collection handle properly and rendering it in the order confirmation email?

 

0 Likes