Send an email alert if a new collection has no orders after x days with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Send an email alert if a new collection has no orders after x days

by Isaac Bowen (team@usemechanic.com)

This task watches for newly-created collections, waits a configurable number of days, and then sends a staff email if no orders have been placed for products that are in the new collection.

Runs 2 days after a collection is created. Configuration includes email recipient, email subject, email body, and days to wait before checking.

15-day free trial – unlimited tasks

Documentation

This task watches for newly-created collections, waits a configurable number of days, and then sends a staff email if no orders have been placed for products that are in the new collection.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Gurus, 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.

Events
2 days after a collection is created (shopify/collections/create+2.days)
Options
email recipient (required, email), email subject (required), email body (required, multiline), days to wait before checking (number, required)
Script
{% assign collection_created_at_s = collection.created_at | date: "%s" | times: 1 %}
{% assign collection_has_orders = false %}

{% assign cursor = nil %}
{% assign total_inventory = 0 %}

{% for n in (0..10000) %}
  {% capture query %}
    query {
      orders(
        first: 10
        after: {{ cursor | json }}
        sortKey: CREATED_AT
        reverse: true
      ) {
        edges {
          node {
            id
            createdAt
            lineItems(
              first: 45
            ) {
              edges {
                node {
                  product {
                    inCollection(id: {{ collection.admin_graphql_api_id | json }})
                  }
                }
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% for order_edge in result.data.orders.edges %}
    {% assign order = order_edge.node %}
    {% assign products_in_order = order.lineItems.edges | map: "node" | map: "product" %}
    {% for product in products_in_order %}
      {% if product.inCollection %}
        {% assign collection_has_orders = true %}
        {% break %}
      {% endif %}
    {% endfor %}
  {% endfor %}

  {% if collection_has_orders %}
    {% break %}
  {% endif %}

  {% assign last_order_created_at_s = result.data.orders.edges.last.node.createdAt | date: "%s" | times: 1 %}
  {% if last_order_created_at_s <= collection_created_at_s %}
    {% break %}
  {% endif %}

  {% if result.data.orders.pageInfo.hasNextPage %}
    {% assign cursor = result.data.orders.edges.last.cursor %}
  {% else %}
    {% break %}
  {% endif %}
{% endfor %}

{% if event.preview or collection_has_orders == false %}
  {% action "email" %}
    {
      "to": {{ options.email_recipient__required_email | json }},
      "subject": {{ options.email_subject__required | json }},
      "body": {{ options.email_body__required_multiline | newline_to_br | json }},
      "reply_to": {{ shop.customer_email | json }},
      "from_display_name": {{ shop.name | json }}
    }
  {% endaction %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Email subject
The new collection "{{ collection.title }}" has no orders yet
Email body
Hello,

The collection "<a href="https://{{ shop.domain }}/collections/{{ collection.handle }}">{{ collection.title }}</a>" was created on {{ collection.created_at | date: "%Y-%m-%d" }}, and no orders have yet been made for products in this collection.

Thanks,
Mechanic, for {{ shop.name }}
Days to wait before checking
2