Receive email alerts when inventory levels change
Use this task to receive immediate email notifications whenever inventory/stock levels are adjusted, whether manually or as a result of a transfer or customer order.
Try this taskWhen it runs
- Occurs whenever an inventory level is updated
- Occurs whenever an inventory level is connected
What you choose
- Email recipient
- Email subject
- Email body
Documentation
Use this task to receive immediate email notifications whenever inventory/stock levels are adjusted, whether manually or as a result of a transfer or customer order.
Please note: Mechanic will start sending emails the second time inventory is adjusted for a particular item.
The following "replacement" variables are defined in the task code for usage within the email subject and body. If you wish to include any other additional information, then you will either need to modify the task code to create additional variables, or use a GraphQL query directly in the task option field.
- AVAILABLE
- DIFFERENCE
- INVENTORY_HISTORY_LINK
- LOCATION_NAME
- MANAGE_VARIANT_LINK
- SKU
- TITLE
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
shopify/inventory_levels/connect
How task subscriptions work
Option types and flags
- Email recipient (email, required)
- Email subject (required)
- Email body (required, multiline)
Task code
{% assign email_recipient = options.email_recipient__email_required %}
{% assign email_subject = options.email_subject__required %}
{% assign email_body = options.email_body__required_multiline %}
{% if event.preview %}
{% capture inventory_level_json %}
{
"inventory_item_id": 1234567890,
"location_id": 1234567890,
"available": 5,
"admin_graphql_api_id": "gid://shopify/InventoryLevel/1234567890?inventory_item_id=1234567890"
}
{% endcapture %}
{% assign inventory_level = inventory_level_json | parse_json %}
{% endif %}
{% assign cache_key = "inventory_level:" | append: inventory_level.inventory_item_id | append: "/" | append: inventory_level.location_id %}
{% assign previous_available = cache[cache_key] %}
{% if event.preview %}
{% assign previous_available = 9 %}
{% endif %}
{% action "cache", "set", cache_key, inventory_level.available %}
{% if previous_available == blank %}
{% break %}
{% endif %}
{% assign available_difference = inventory_level.available | minus: previous_available %}
{% if available_difference >= 0 %}
{% assign available_difference = "+" | append: available_difference %}
{% endif %}
{% comment %}
-- get additional data to be used in email content substitutions
{% endcomment %}
{% capture query %}
query {
inventoryLevel(id: {{ inventory_level.admin_graphql_api_id | json }}) {
location {
name
}
item {
inventoryHistoryUrl
sku
variants(first: 1) {
nodes {
legacyResourceId
displayName
product {
legacyResourceId
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"inventoryLevel": {
"location": {
"name": "Warehouse A"
},
"item": {
"inventoryHistoryUrl": "https://preview-sample.myshopify.com/admin/products/inventory/1234567890/inventory_history",
"sku": "WDGT123",
"variants": {
"nodes": [
{
"legacyResourceId": "1234567890",
"displayName": "Widget - Default Title",
"product": {
"legacyResourceId": "987654321"
}
}
]
}
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign location_name = result.data.inventoryLevel.location.name %}
{% assign sku = result.data.inventoryLevel.item.sku %}
{% assign variant = result.data.inventoryLevel.item.variants.nodes.first %}
{% assign title = variant.displayName | remove: " - Default Title" %}
{% assign variant_id = variant.legacyResourceId %}
{% assign product_id = variant.product.legacyResourceId %}
{%- capture inventory_history_link -%}
<a href="{{ result.data.inventoryLevel.item.inventoryHistoryUrl }}?location_id={{ inventory_level.location_id }}">View inventory history</a>
{%- endcapture -%}
{%- capture manage_variant_link -%}
<a href="{{ shop.admin_url }}products/{{ product_id }}/variants/{{ variant_id }}">Manage this variant</a>
{%- endcapture -%}
{% assign email_subject
= email_subject
| replace: "DIFFERENCE", available_difference
| replace: "SKU", sku
| replace: "TITLE", title
%}
{% assign email_body
= email_body
| replace: "AVAILABLE", inventory_level.available
| replace: "DIFFERENCE", available_difference
| replace: "INVENTORY_HISTORY_LINK", inventory_history_link
| replace: "LOCATION_NAME", location_name
| replace: "MANAGE_VARIANT_LINK", manage_variant_link
%}
{% action "email" %}
{
"to": {{ email_recipient | json }},
"subject": {{ email_subject | strip | json }},
"body": {{ email_body | strip | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
Mechanic Liquid reference
Default option values
- Email subject
DIFFERENCE inventory for SKU TITLE- Email body
Currently available inventory: AVAILABLE Changed by: DIFFERENCE Location: LOCATION_NAME INVENTORY_HISTORY_LINK MANAGE_VARIANT_LINK Thanks, {{ shop.name }}