[Shopify] Separate order email content by collection.

There was a requirement to customise the email text in Shopify order completion emails only when a product belonging to a specific collection was purchased, which was addressed.

目次

Changing the text of notification emails

The first step is to describe how to change the normal text.

Click Settings > Notifications > Customer notifications.

A list is displayed and you click ‘Confirm order’.

A preview of the email template is displayed, click ‘Edit code’.

Click on this and you will see the Liquid code and simply add text as you wish.

The caveat here is that you need to change it in accordance with Liquid’s modus operandi.
If it is unintentionally broken, click ‘Revert to defaults’ at the bottom to cut it back.

Customising notification emails

We will customise the email text so that it can only be changed when a product belonging to a specific collection is purchased.

First, we define the variable special_template_used, which manages the status.

<!-- Add text when buying specific collections. -->
{% assign special_template_used = false %}

Get the list of ordered products from order.line_items.
From there, for each product, get the name of the collection it belongs to in collection.handle. Here, if the product belongs to a specific collection, the state management variable is updated.

{% for line_item in order.line_items %}
  {% assign product_collections = line_item.product.collections %}
  
  {% for collection in product_collections %}
    {% if collection.handle == 'collection_name' %}
      {% assign special_template_used = true %}
    {% endif %}
  {% endfor %}
{% endfor %}

You can then change the text and HTML to be displayed based on the special_template_used variable.

<!-- Email content when a product belongs to a specific collection. -->
{% if special_template_used %}
    Special content offered exclusively to those who have purchased the product!<br>
    <a href="https://xxx" target="_blank">URL</a>
{% endif %}

The Notification Relations Reference will help you to do many more things.
Have a read.

https://help.shopify.com/ja/manual/fulfillment/setup/notifications/email-variables

Summary

Shopify can be customised in a variety of ways in the Liquid language, which we would like to make use of.

よかったらシェアしてね!
目次