Auto-tag out-of-stock products with Mechanic.

Mechanic is an automation and development platform for Shopify. :)

Auto-tag out-of-stock products

by Isaac Bowen (team@usemechanic.com)

Does what it says on the tin. :) Optionally, emails you an alert when a product goes out of stock.

Runs when an inventory level is updated and when a user triggers the task. Configuration includes out of stock inventory level, product tag, and email alert recipient.

15-day free trial – unlimited tasks

Documentation

Run this task manually to scan and tag all products in your store, according to their respective total inventory levels. Fill in the email alert recipient to send an email alert when a product goes out of stock.

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)
when a user triggers the task (mechanic/user/trigger)
Options
out of stock inventory level (number, required), product tag (required), email alert recipient (email)
Script
{% assign products = array %}

{% if event.preview %}
  {% assign product = '{"title": "A Shoe", "variants": [{"inventory_quantity": 5}], "admin_graphql_api_id": "gid://shopify/Product/1234567890"}' | parse_json %}
  {% assign products[products.size] = product %}
{% elsif event.topic contains "shopify/products/" %}
  {% assign products[products.size] = product %}
{% elsif event.topic contains "shopify/inventory_levels/" %}
  {% assign products[products.size] = inventory_level.variant.product %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign products = shop.products %}
{% endif %}

{% for product in products %}
  {% assign product_inventory = product.variants | map: "inventory_quantity" | sum %}

  {% assign product_tags = product.tags | split: ", " %}

  {% if event.preview or product_inventory <= options.out_of_stock_inventory_level__number_required %}
    {% unless event.preview != true and product_tags contains options.product_tag__required %}
      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ product.admin_graphql_api_id | json }}
            tags: {{ options.product_tag__required | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}

      {% if event.topic != "mechanic/user/trigger" and options.email_alert_recipient__email != blank %}
        {% capture email_body %}
          Hello,

          {{ product.title }} is now out of stock, with a total stock level of {{ product_inventory }} (across all variants and locations).

          <a href="https://{{ shop.domain }}/admin/products/{{ product.id }}">Manage this product</a>

          Thanks,
          Mechanic (for {{ shop.name }})
        {% endcapture %}

        {% action "email" %}
          {
            "to": {{ options.email_alert_recipient__email | json }},
            "subject": {{ product.title | append: " is now out of stock (" | append: product_inventory | append: ")" | json }},
            "body": {{ email_body | unindent | strip | newline_to_br | json }},
            "reply_to": {{ shop.customer_email | json }},
            "from_display_name": {{ shop.name | json }}
          }
        {% endaction %}
      {% endif %}
    {% endunless %}
  {% else %}
    {% if product_tags contains options.product_tag__required %}
      {% action "shopify" %}
        mutation {
          tagsRemove(
            id: {{ product.admin_graphql_api_id | json }}
            tags: {{ options.product_tag__required | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endif %}
{% endfor %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Out of stock inventory level
0
Product tag
out-of-stock