Shopify: How to Send Different Emails Based on Products

Image from Pexels

Image from Pexels

In order to send custom messages in your emails based on the the product that someone has purchased, you’re going to need to modify your Shopify notification template.

Your notification templates are located at /admin/settings/notifications.

In this example, we’re going to be creating different email content for a product called “SIMPLE-WEB-1”.

First, we will want to create a variable. To do this, we simply write this code at the top of your email notification template:

{% assign simple_web_product_1 = false %}

What we’re doing with that piece of code is creating a liquid variable (liquid is the programming language that Shopify uses) and assigning it to FALSE. We assign it to FALSE to begin with because later on we’re going to check the product title and if we find a matching title we’ll assign it to TRUE.

Now to check the list of product titles, we’re going to loop through all of the line items. A line item is basically a single line in the users shopping cart. So if they purchase 4 different products, there will be 4 line items.

To loop through them, we’ll use the below code:

{% for line in line_items %}
  {% if line.title contains 'SIMPLE-WEB-1' %}
  {% endif %}
{% endfor %}

The ‘for’ is the loop, and is basically saying for each of those line items we’re going to do the code that is wrapped inside the for and endfor.

Now at the moment, the above code doesn’t really do anything because there’s nothing inbetween the if and endif statements. That is to say, if we do find a product with the title in Shopify called “SIMPLE-WEB-1”, we haven’t got any code that’s going to run yet. So, what we want to do is add in a line to assign the variable we setup earlier to TRUE.

{% for line in line_items %}
  {% if line.title contains 'SIMPLE-WEB-1' %}
    {% assign simple_web_product_1 = true %}
  {% endif %}
{% endfor %}

So our code at the moment is looping through all of the line items and if it finds one with the product title = ‘SIMPLE-WEB-1’, we’re assigning a variable to TRUE. The reason behind setting this in a variable is so that later on in our email copy we can check to see if the variable is TRUE or not.

Next up, we’re going to capture the content that you want to display if someone has purchased the product ‘SIMPLE-WEB-1’.
To do this, we use capture to create a variable. In the below example “simple_web_email_1” is the new variable we’ve just made.

{% capture simple_web_email_1 %} Thanks for purchasing this product. There is currently a small delay in shipping. We expect orders to be sent out by 21st February {% endcapture %}

Now that we’ve got our variable and content for the email, we just need to add the IF statement in to check for it — and then put it all together.

{% if simple_web_product_1 == true %}
{{ simple_web_email_1 }}
{% endif %}

You can place the above code wherever you would like in your email. This is basically where the content is going to show to your customers if they have purchased the product “SIMPLE-WEB-1”. You will most likely want to place it somewhere as in the below picture (between the two {% endif %} lines).

2021-01-31_18-07-56.jpeg

Now here is the full code below, you just need to replace the endcapture and above on your email template with this. You can repeat this with as many products as you need, and adjust any of the variable names.

{% assign simple_web_product_1 = false %}
{% for line in line_items %}
  {% if line.title contains 'SIMPLE-WEB-1' %}
    {% assign simple_web_product_1 = true %}
  {% endif %}
{% endfor %}
{% capture simple_web_email_1 %} Thanks for purchasing this product. There is currently a small delay in shipping. We expect orders to be sent out by 21st February {% endcapture %}
{% capture email_title %}Thank you for your purchase! {% endcapture %}
{% capture email_body %}
  {% if requires_shipping %}
  {% case delivery_method %}
      {% when 'pick-up' %}
        You’ll receive an email when your order is ready for pickup.
      {% when 'local' %}
        Hi {{ customer.first_name }}, we're getting your order ready for delivery.
      {% else %}
        Hi {{ customer.first_name }}, we're getting your order ready to be shipped. We will notify you when it has been sent.
    {% endcase %}
      {% if delivery_instructions != blank  %}
        <p><b>Delivery information:</b> {  }</p>
      {% endif %}
      {% if simple_web_product_1 == true %}
        {{ simple_web_email_1 }}
      {% endif %}
  {% endif %}
{% endcapture %}