Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

Occurs whenever an order is paid

What you choose

  • Required collection ID
  • Email address
  • Email subject
  • Email body

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.

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

shopify/orders/paid
How task subscriptions work

Option types and flags

Task 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 %}
Mechanic Liquid reference

Default option values

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 }}