Tag products as in- or out-of-stock, with Mechanic.

Mechanic is a development platform for Shopify. :)

Tag products as in- or out-of-stock

by Isaac Bowen (team@usemechanic.com)

This task scans your products, in bulk, and tags them according to whether or not they're in stock. Optionally, configure this task to monitor products for live auto-tagging.

Runs when a user triggers the task and when a bulk operation finishes. Configuration includes in stock tag, out of stock tag, and monitor products for inventory updates.

15-day free trial – unlimited tasks

Documentation

This task scans your products, in bulk, and tags them according to whether or not they're in stock. Optionally, configure this task to monitor products for live auto-tagging.

For the purposes of this task, "in stock" means "having a total inventory of greater than zero, when added up for all variants across all locations".

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 a user triggers the task (mechanic/user/trigger)
when a bulk operation finishes (mechanic/shopify/bulk_operation)
Options
in stock tag (required), out of stock tag (required), monitor products for inventory updates (boolean)
Script
{% assign products = array %}

{% if event.topic contains "shopify/products/" %}
  {% if event.preview %}
    {% capture product_json %}
      {
        "tags": "",
        "admin_graphql_api_id": "gid:\/\/shopify\/Product\/788032119674292922",
        "variants": [
          {
            "inventory_quantity": 6
          },
          {
            "inventory_quantity": -5
          }
        ]
      }

    {% endcapture %}

    {% assign product = product_json | parse_json %}
  {% endif %}

  {% assign graphql_product = hash %}
  {% assign graphql_product["id"] = product.admin_graphql_api_id %}
  {% assign graphql_product["tags"] = product.tags | split: ", " %}
  {% assign graphql_product["totalInventory"] = product.variants | map: "inventory_quantity" | sum %}

  {% assign products[0] = graphql_product %}
{% elsif event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {% capture bulk_operation_query %}
    query {
      products {
        edges {
          node {
            id
            totalInventory
            tags
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% if event.preview %}
    {% capture bulkOperation_objects_jsonl %}
      {"id":"gid:\/\/shopify\/Product\/1234567890","totalInventory":1,"tags":[]}
    {% endcapture %}

    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = bulkOperation_objects_jsonl | parse_jsonl %}
  {% endif %}

  {% assign products = bulkOperation.objects %}
{% endif %}

{% for product in products %}
  {% assign tag_to_add = nil %}
  {% assign tag_to_remove = nil %}

  {% if product.totalInventory > 0 %}
    {% unless product.tags contains options.in_stock_tag__required %}
      {% assign tag_to_add = options.in_stock_tag__required %}
    {% endunless %}

    {% if product.tags contains options.out_of_stock_tag__required %}
      {% assign tag_to_remove = options.out_of_stock_tag__required %}
    {% endif %}
  {% else %}
    {% unless product.tags contains options.out_of_stock_tag__required %}
      {% assign tag_to_add = options.out_of_stock_tag__required %}
    {% endunless %}

    {% if product.tags contains options.in_stock_tag__required %}
      {% assign tag_to_remove = options.in_stock_tag__required %}
    {% endif %}
  {% endif %}

  {% if tag_to_add or tag_to_remove %}
    {% action "shopify" %}
      mutation {
        {% if tag_to_add %}
          tagsAdd(
            id: {{ product.id | json }}
            tags: {{ tag_to_add | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        {% endif %}
        {% if tag_to_remove %}
          tagsRemove(
            id: {{ product.id | json }}
            tags: {{ tag_to_remove | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        {% endif %}
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
In stock tag
in-stock
Out of stock tag
out-of-stock