Send an email alert if a new collection has no orders after x days
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.
Try this taskWhen it runs
Occurs whenever a collection is created, with a 2 day delay
What you choose
- Email recipient
- Email subject
- Email body
- Days to wait before checking
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/collections/create+{{ options.days_to_wait_before_checking__number_required | default: 2 }}.days
How task subscriptions work
Option types and flags
- Email recipient (required, email)
- Email subject (required)
- Email body (required, multiline)
- Days to wait before checking (number, required)
Task code
{% 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 Liquid reference
Default option values
- 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