Send an email alert when a variant hits 0 total inventory
Use this task to receive an immediate email alert, whenever a variant's total inventory hits 0, when added up across all locations. You can limit alerts to a specific collection or publication (e.g. Online Store or Point of Sale).
Try this taskWhen it runs
- Occurs whenever an inventory level is updated
- Occurs when a user manually triggers the task
What you choose
- Must belong to collection
- Must be published to publication ID
- Email subject
- Email body
- Email recipient
Documentation
Use this task to receive an immediate email alert, whenever a variant's total inventory hits 0, when added up across all locations. You can limit alerts to a specific collection or publication (e.g. Online Store or Point of Sale).
Notes:
- You can filter by collection or publication, not both at the same time.
- Run the task manually to get the list of publication IDs for your shop.
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/inventory_levels/update
mechanic/user/trigger
How task subscriptions work
Option types and flags
- Must belong to collection (picker, collection)
- Email subject (required)
- Email body (multiline, required)
- Email recipient (email, required)
Task code
{% assign must_belong_to_collection_id = options.must_belong_to_collection__picker_collection %}
{% assign must_be_published_to_publication_id = options.must_be_published_to_publication_id %}
{% assign email_subject = options.email_subject__required %}
{% assign email_body = options.email_body__multiline_required %}
{% assign email_recipient = options.email_recipient__email_required %}
{% if must_belong_to_collection_id != blank and must_be_published_to_publication_id != blank %}
{% error "You can only filter by collection or publication, not both." %}
{% endif %}
{% if event.topic == "mechanic/user/trigger" %}
{% comment %}
-- run the task manually to get the list of your publications and their IDs
{% endcomment %}
{% capture query %}
query {
publications(
first: 250
catalogType:APP
) {
nodes {
id
catalog {
... on AppCatalog {
apps(first: 1) {
nodes {
title
}
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"publications": {
"nodes": [
{
"id": "gid://shopify/Publication/1234567890",
"catalog": {
"apps": {
"nodes": [
{
"title": "Online Store"
}
]
}
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign publication_names_by_id = hash %}
{% for publication in result.data.publications.nodes %}
{% assign publication_id = publication.id | remove: "gid://shopify/Publication/" %}
{% assign publication_names_by_id[publication_id] = publication.catalog.apps.nodes.first.title %}
{% endfor %}
{% log publication_names_by_id %}
{% elsif event.topic == "shopify/inventory_levels/update" %}
{% capture query %}
query {
inventoryLevel(id: {{ inventory_level.admin_graphql_api_id | json }}) {
item {
variants(first: 1) {
nodes {
displayName
inventoryQuantity
legacyResourceId
product {
legacyResourceId
{% if must_belong_to_collection_id -%}
inCollection(id: {{ must_belong_to_collection_id | json }})
{%- elsif must_be_published_to_publication_id -%}
publishedOnPublication(publicationId: "gid://shopify/Publication/{{must_be_published_to_publication_id}}")
{%- endif %}
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"inventoryLevel": {
"item": {
"variants": {
"nodes": [
{
"displayName": "Short sleeve t-shirt - L / Red",
"inventoryQuantity": 0,
"legacyResourceId": "1234567890",
"product": {
"legacyResourceId": "1234567890"
{% if must_belong_to_collection_id -%}
, "inCollection" : "true"
{%- elsif options.must_be_published_to_publication_id -%}
, "publishedOnPublication": "true"
{%- endif %}
}
}
]
}
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign variant = result.data.inventoryLevel.item.variants.nodes.first %}
{% assign variant_title = variant.displayName | remove: " - Default Title" %}
{% assign product_qualifies = false %}
{% if must_belong_to_collection_id == blank and must_be_published_to_publication_id == blank %}
{% assign product_qualifies = true %}
{% elsif must_belong_to_collection_id and variant.product.inCollection %}
{% assign product_qualifies = true %}
{% elsif must_be_published_to_publication_id and variant.product.publishedOnPublication %}
{% assign product_qualifies = true %}
{% endif %}
{% if variant.inventoryQuantity == 0 and product_qualifies %}
{%- capture variant_admin_url -%}
{{ shop.admin_url }}products/{{ variant.product.legacyResourceId }}/variants/{{ variant.legacyResourceId }}
{%- endcapture -%}
{% assign email_subject
= email_subject
| replace: "VARIANT_TITLE", variant_title
| replace: "VARIANT_ADMIN_URL", variant_admin_url
%}
{% assign email_body
= email_body
| replace: "VARIANT_TITLE", variant_title
| replace: "VARIANT_ADMIN_URL", variant_admin_url
%}
{% action "email" %}
{
"to": {{ email_recipient | json }},
"subject": {{ email_subject | json }},
"body": {{ email_body | strip | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% endif %}
{% endif %}
Mechanic Liquid reference
Default option values
- Email subject
"VARIANT_TITLE" has hit 0 inventory- Email body
Hello, The variant "VARIANT_TITLE" is now at 0 inventory, totaled across all locations. <a href="VARIANT_ADMIN_URL">Manage this variant in Shopify</a> Thanks, Mechanic, for {{ shop.name }}