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

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

Auto-tag orders that contain an out of stock item

This task watches for new orders, and checks each line item for that variant's total inventory quantity. If any are found with an inventory level of 0 or less, the task will add the tag of your choice to the order, and optionally will add a tag to the customer and to each product related to an out-of-stock line item.

Runs Occurs whenever an order is created. Configuration includes apply this order tag, ignore variants marked for oversell, apply this customer tag, and apply this product tag for each out of stock line item.

15-day free trial – unlimited tasks

Documentation

This task watches for new orders, and checks each line item for that variant's total inventory quantity. If any are found with an inventory level of 0 or less, the task will add the tag of your choice to the order, and optionally will add a tag to the customer and to each product related to an out-of-stock line item.

By default, this task will disregard the inventory levels of any variants which have the "Continue selling when out of stock" option enabled. Uncheck the "Ignore variants marked for oversell" task option to disable this exclusion.

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/orders/create
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
apply this order tag (required) , ignore variants marked for oversell (boolean) , apply this customer tag , apply this product tag for each out of stock line item
Code
{% assign apply_this_order_tag = options.apply_this_order_tag__required %}
{% assign ignore_variants_marked_for_oversell = options.ignore_variants_marked_for_oversell__boolean %}
{% assign apply_this_customer_tag = options.apply_this_customer_tag %}
{% assign apply_this_product_tag = options.apply_this_product_tag_for_each_out_of_stock_line_item %}

{% comment %}
  -- get product and variant data for all line items on this order
{% endcomment %}

{% assign out_of_stock_products = array %}

{% capture query %}
  query {
    order(id: {{ order.admin_graphql_api_id | json }}) {
      id
      tags
      {% if apply_this_customer_tag != blank %}
        customer {
          id
          tags
        }
      {% endif %}
      lineItems(first: 250) {
        nodes {
          name
          product {
            id
            title
            tags
            tracksInventory
          }
          variant {
            inventoryQuantity
            inventoryPolicy
          }
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "order": {
          "id": "gid://shopify/Order/1234567890",
          "customer": {
            "id": "gid://shopify/Customer/1234567890"
          },
          "lineItems": {
            "nodes": [
              {
                "name": "An out of stock product",
                "product": {
                  "id": "gid://shopify/Product/1234567890",
                  "title": "An out of stock product",
                  "tracksInventory" : true
                },
                "variant": {
                  "inventoryQuantity": -1,
                  "inventoryPolicy": "DENY"
                }
              }
            ]
          }
        }
      }
    }
  {% endcapture %}

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

{% assign order = result.data.order %}

{% for line_item in order.lineItems.nodes %}
  {% assign product = line_item.product %}
  {% assign variant = line_item.variant %}

  {% comment %}
    -- skip custom line items (i.e. not attached to a product in the shop), and products that do not track inventory
  {% endcomment %}

  {% if product == blank %}
    {% continue %}
  {% endif %}

  {% unless product.tracksInventory %}
    {% continue %}
  {% endunless %}

  {% if variant.inventoryQuantity < 0 %}
    {% comment %}
      -- ignore variants with negative inventory if they are set to oversell and that option is enabled in the task
    {% endcomment %}

    {% if ignore_variants_marked_for_oversell and variant.inventoryPolicy == "CONTINUE" %}
      {%- capture log_output -%}
        {{ line_item.name }} was purchased while at stock level {{ variant.inventoryQuantity }}; however, the variant's inventory policy allows overselling and the "Ignore variants marked for oversell" task option is enabled; skipping.
      {%- endcapture -%}

      {% log log_output %}

      {% continue %}
    {% endif %}

    {% assign out_of_stock_products = out_of_stock_products | push: product | uniq %}
  {% endif %}
{% endfor %}

{% if out_of_stock_products == blank %}
  {% log "No out of stock products for this order." %}
  {% break %}
{% endif %}

{% comment %}
  -- apply the order tag
{% endcomment %}

{% action "shopify" %}
  mutation {
    tagsAdd(
      id: {{ order.id | json }}
      tags: {{ apply_this_order_tag | json }}
    ) {
      node {
        ... on Order {
          id
          name
        }
      }
      userErrors {
        field
        message
      }
    }
  }
{% endaction %}

{% comment %}
  -- apply the customer tag if it is configured, there is a customer for the order, and they do not yet have the tag
{% endcomment %}

{% unless apply_this_customer_tag == blank or order.customer == blank %}
  {% if order.customer.tags contains apply_this_customer_tag %}
    {% log "This customer already has the configured tag." %}

  {% else %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ order.customer.id | json }}
          tags: {{ apply_this_customer_tag | json }}
        ) {
          node {
            ... on Customer {
              id
              displayName
            }
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endunless %}

{% comment %}
  -- apply product tag if it is configured, for any products that do not yet have the tag
{% endcomment %}

{% if apply_this_product_tag != blank %}
  {% for product in out_of_stock_products %}
    {% if product.tags contains apply_this_product_tag %}
      {%- capture log_output -%}
        The product "{{ product.title }}" already has the configured tag.
      {%- endcapture -%}

      {% log log_output %}

    {% else %}
      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ product.id | json }}
            tags: {{ apply_this_product_tag | json }}
          ) {
            node {
              ... on Product {
                id
                title
              }
            }
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endfor %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Ignore variants marked for oversell
true