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

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

Tag products as in- or out-of-stock

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 Occurs when a user manually triggers the task and Occurs when a bulk operation is completed. 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, 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
mechanic/user/trigger
mechanic/shopify/bulk_operation

{% if options.monitor_products_for_inventory_updates__boolean %}
  shopify/products/create
  shopify/products/update
{% endif %}
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
in stock tag (required), out of stock tag (required), monitor products for inventory updates (boolean)
Code
{% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
In stock tag
in-stock
Out of stock tag
out-of-stock