Send an email when a purchase is made from a certain collection, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

Send an email when a purchase is made from a certain collection

This task will monitor incoming orders for any products that are included in the collection you choose. Learn where to find the collection ID.

Runs Occurs whenever an order is paid. Configuration includes required collection id, email address, email subject, and email body.

15-day free trial – unlimited tasks

Documentation

This task will monitor incoming orders for any products that are included in the collection you choose. Learn where to find the collection ID.

Please note: This task does not determine what collection the customer may have been browsing when they added the product to the cart. This task simply checks to see which collections contain each purchased product, looking for a match with the collection you choose.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, everybody.

That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
shopify/orders/paid
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
required collection id (number, required), email address (required), email subject (required), email body (multiline, required)
Code
{% capture query %}
  query {
    collection(id: "gid://shopify/Collection/{{ options.required_collection_id__number_required }}") {
      id
      title
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "collection": {
          "id": "gid://shopify/Collection/{{ options.required_collection_id__number_required }}",
          "title": "[sample collection]"
        }
      }
    }
  {% endcapture %}

  {% assign result = result_json | parse_json %}
{% endif %}

{% assign collection = result.data.collection %}

{% unless collection %}
  {% error message: "Couldn't find a collection with the configured ID", collection_id: options.required_collection_id__number_required %}
  {% break %}
{% endunless %}

{% capture query %}
  query {
    order(id: {{ order.admin_graphql_api_id | json }}) {
      lineItems(first: 250) {
        edges {
          node {
            id
            title
            product {
              inCollection(id: {{ collection.id | json }})
            }
          }
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "order": {
          "lineItems": {
            "edges": [
              {
                "node": {
                  "id": "gid://shopify/LineItem/1234567890",
                  "title": "[sample product]",
                  "product": {
                    "inCollection": true
                  }
                }
              }
            ]
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = result_json | parse_json %}
{% endif %}

{% assign qualifying_line_item_titles = array %}

{% for lineItem_edge in result.data.order.lineItems.edges %}
  {% if lineItem_edge.node.product.inCollection %}
    {% assign qualifying_line_item_titles[qualifying_line_item_titles.size] = lineItem_edge.node.title %}
  {% endif %}
{% endfor %}

{% if qualifying_line_item_titles == empty %}
  {% log message: "No line items found from the configured collection", collection: collection, order: result.data.order %}
  {% break %}
{% endif %}

{% assign line_item_titles = qualifying_line_item_titles | uniq | join: ", " %}
{% assign collection_title = collection.title %}

{% action "email" %}
  {
    "to": {{ options.email_address__required | json }},
    "subject": {{ options.email_subject__required | replace: "COLLECTION_TITLE", collection_title | replace: "LINE_ITEM_TITLES", line_item_titles | json }},
    "body": {{ options.email_body__multiline_required | replace: "COLLECTION_TITLE", collection_title | replace: "LINE_ITEM_TITLES", line_item_titles | strip | newline_to_br | json }},
    "from_display_name": {{ shop.name | json }}
  }
{% endaction %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Email subject
New purchase for LINE_ITEM_TITLES, from COLLECTION_TITLE
Email body
Hello,

Order {{ order.name }} includes LINE_ITEM_TITLES, from COLLECTION_TITLE.

<a href="https://{{ shop.domain }}/admin/orders/{{ order.id }}">Manage this order in Shopify</a>

Thanks,
-Mechanic, for {{ shop.name }}