Send an email alert when a variant hits 0 total inventory, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Send an email alert when a variant hits 0 total inventory

by Isaac Bowen (team@usemechanic.com)

Use this task to receive an immediate email alert, whenever a variant's total inventory hits 0, when added up across all locations.

Runs when an inventory level is updated. Configuration includes email subject, email body, and email recipient.

15-day free trial – unlimited tasks

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
when an inventory level is updated (shopify/inventory_levels/update)
Options
email subject (required), email body (multiline, required), email recipient (email, required)
Script
{% capture query %}
  query {
    inventoryLevel(id: {{ inventory_level.admin_graphql_api_id | json }}) {
      item {
        variant {
          displayName
          inventoryQuantity
          legacyResourceId
          product {
            legacyResourceId
          }
        }
      }
    }
  }
{% 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"
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = result_json | parse_json %}
{% endif %}

{% assign variantNode = result.data.inventoryLevel.item.variant %}

{% if variantNode.inventoryQuantity == 0 %}
  {% 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 %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
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 }}