Auto-tag orders that contain an out of stock item, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-tag orders that contain an out of stock item

by Isaac Bowen (team@usemechanic.com)

This task watches for newly-created orders, and checks each line item for that variant's total inventory quantity. If the inventory quantity is 0 or less, the task will add the tag of your choice to the order (unless the order already has that tag).

Runs when an order is created. Configuration includes apply this order tag.

15-day free trial – unlimited tasks

Documentation

This task watches for newly-created orders, and checks each line item for that variant's total inventory quantity. If the inventory quantity is 0 or less, the task will add the tag of your choice to the order (unless the order already has that tag).

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 order is created (shopify/orders/create)
Options
apply this order tag (required)
Script
{% assign cursor = nil %}
{% assign out_of_stock_purchase = false %}

{% for n in (0..100) %}
  {% capture query %}
    query {
      order(id: {{ order.admin_graphql_api_id | json }}) {
        tags
        lineItems(
          first: 250
          after: {{ cursor | json }}
        ) {
          pageInfo {
            hasNextPage
          }
          edges {
            cursor
            node {
              name
              variant {
                inventoryQuantity
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if result.data.order.tags contains options.apply_this_order_tag__required %}
    {"log": "This order is already tagged."}
    {% break %}
  {% endif %}

  {% for lineItem_edge in result.data.order.lineItems.edges %}
    {% assign variant = lineItem_edge.node.variant %}
    {% if variant and variant.inventoryQuantity <= 0 %}
      {"log": {{ lineItem_edge.node.name | append: " was purchased while at stock level " | append: variant.inventoryQuantity | json }}}
      {% assign out_of_stock_purchase = true %}
      {% break %}
    {% endif %}
  {% endfor %}

  {% if result.data.order.lineItems.pageInfo.hasNextPage %}
    {% assign cursor = result.data.order.lineItems.edges.last.cursor %}
  {% else %}
    {% break %}
  {% endif %}
{% endfor %}

{% if event.preview or out_of_stock_purchase %}
  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: {% if event.preview %}"gid://shopify/Order/1234567890"{% else %}{{ order.admin_graphql_api_id | json }}{% endif %}
        tags: {{ options.apply_this_order_tag__required | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.