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

Mechanic is a development and ecommerce automation platform for Shopify. :)

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).

Runs Occurs whenever an inventory level is updated and Occurs when a user manually triggers the task. Configuration includes must belong to collection id, must be published to publication id, email subject, email body, and email recipient.

15-day free trial – unlimited tasks

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.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, 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.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
shopify/inventory_levels/update
mechanic/user/trigger
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
must belong to collection id, must be published to publication id, email subject (required), email body (multiline, required), email recipient (email, required)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
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 }}