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 POS).
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 ID
- 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 POS).
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
- Email subject (required)
- Email body (multiline, required)
- Email recipient (email, required)
Task code
{% comment %}
Run the task manually to get the list of your publications and their IDs
{% endcomment %}
{% if event.topic == "mechanic/user/trigger" or event.preview %}
{% capture query %}
query {
publications (first: 30 ) {
nodes {
name
id
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% action "echo" result %}
{% endif %}
{% if event.topic == "shopify/inventory_levels/update" or event.preview %}
{% capture query %}
query {
inventoryLevel(id: {{ inventory_level.admin_graphql_api_id | json }}) {
item {
variant {
displayName
inventoryQuantity
legacyResourceId
product {
legacyResourceId
{% if options.must_belong_to_collection_id -%}
inCollection(id: "gid://shopify/Collection/{{options.must_belong_to_collection_id}}")
{%- endif %}
{% if options.must_be_published_to_publication_id -%}
publishedOnPublication(publicationId: "gid://shopify/Publication/{{options.must_be_published_to_publication_id}}")
{%- endif %}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"inventoryLevel": {
"item": {
"variant": {
"displayName": "Short sleeve t-shirt - L / Red",
"inventoryQuantity": 0,
"legacyResourceId": "1234567890",
"product": {
"legacyResourceId": "1234567890"
{% if options.must_belong_to_collection_id -%}
, "inCollection" : "true"
{%- endif %}
{% if options.must_be_published_to_publication_id -%}
, "publishedOnPublication": "true"
{%- endif %}
}
}
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% action "echo" result %}
{% endif %}
{% assign product_qualifies = false %}
{% assign variantNode = result.data.inventoryLevel.item.variant %}
{% if options.must_belong_to_collection_id != blank and options.must_be_published_to_publication_id != blank %}
{% error "You can only filter by collection or publication, not both." %}
{% endif %}
{% if options.must_belong_to_collection_id == blank and options.must_be_published_to_publication_id == bank %}
{% assign product_qualifies = true %}
{% elsif options.must_belong_to_collection_id and variantNode.product.inCollection %}
{% assign product_qualifies = true %}
{% elsif options.must_be_published_to_publication_id and variantNode.product.publishedOnPublication %}
{% assign product_qualifies = true %}
{% endif %}
{% if variantNode.inventoryQuantity == 0 and product_qualifies %}
{% assign variant_admin_url = "https://" | append: shop.domain | append: "/admin/products/" | append: variantNode.product.legacyResourceId | append: "/variants/" | append: variantNode.legacyResourceId %}
{% assign subject = options.email_subject__required | replace: "VARIANT_TITLE", variantNode.displayName | replace: "VARIANT_ADMIN_URL", variant_admin_url %}
{% assign body = options.email_body__multiline_required | replace: "VARIANT_TITLE", variantNode.displayName | replace: "VARIANT_ADMIN_URL", variant_admin_url %}
{% action "email" %}
{
"to": {{ options.email_recipient__email_required | json }},
"subject": {{ subject | json }},
"body": {{ 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 }}