Mechanic Get Mechanic
Task library

Tag products as in- or out-of-stock

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

Try this task

When it runs

  • Occurs when a user manually triggers the task
  • Occurs when a bulk operation is completed

What you choose

  • In stock tag
  • Out of stock tag
  • Monitor products for inventory updates

Documentation

This task scans your active products, in bulk, and tags them according to whether or not they're in stock. Optionally, configure this task to monitor inventory changes 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.

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

mechanic/user/trigger
mechanic/shopify/bulk_operation
{% if options.monitor_products_for_inventory_updates__boolean %}
  shopify/inventory_levels/update
{% endif %}
How task subscriptions work

Option types and flags

Task code

{% assign in_stock_tag = options.in_stock_tag__required %}
{% assign out_of_stock_tag = options.out_of_stock_tag__required %}

{% if event.topic == "shopify/inventory_levels/update" %}
  {% comment %}
    -- get product and variant data
    -- sum inventoryQuantity from all variants instead of using product.totalInventory due to calculated field lag
  {% endcomment %}

  {% capture query %}
    query {
      inventoryLevel(id: {{ inventory_level.admin_graphql_api_id | json }}) {
        item {
          variants(first: 1) {
            nodes {
              product {
                id
                status
                totalInventory
                tags
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "inventoryLevel": {
            "item": {
              "variants": {
                "nodes": [
                  {
                    "product": {
                      "id": "gid://shopify/Product/1234567890",
                      "status": "ACTIVE"
                    }
                  }
                ]
              }
            }
          }
        }
      }
    {% endcapture %}

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

  {% assign product = result.data.inventoryLevel.item.variants.nodes.first.product %}

  {% unless product.status == "ACTIVE" %}
    {% break %}
  {% endunless %}

  {% capture query %}
    query {
      product(id: {{ product.id | json }}) {
        variants(first: 2048) {
          nodes {
            inventoryQuantity
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% assign available_inventory = result.data.product.variants.nodes | map: "inventoryQuantity" | sum %}

  {% unless event.preview %}
    {% log
      available_inventory_summed_from_variants: available_inventory,
      product_total_inventory_laggy_calc: product.totalInventory
    %}
  {% endunless %}

  {% assign tag_to_add = nil %}
  {% assign tag_to_remove = nil %}

  {% if available_inventory > 0 %}
    {% unless product.tags contains in_stock_tag %}
      {% assign tag_to_add = in_stock_tag %}
    {% endunless %}

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

  {% else %}
    {% unless product.tags contains out_of_stock_tag %}
      {% assign tag_to_add = out_of_stock_tag %}
    {% endunless %}

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

  {% if tag_to_add != blank or tag_to_remove != blank %}
    {% action "shopify" %}
      mutation {
        {% if tag_to_add != blank %}
          tagsAdd(
            id: {{ product.id | json }}
            tags: {{ tag_to_add | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        {% endif %}
        {% if tag_to_remove != blank %}
          tagsRemove(
            id: {{ product.id | json }}
            tags: {{ tag_to_remove | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        {% endif %}
      }
    {% endaction %}
  {% endif %}

{% elsif event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {% capture bulk_operation_query %}
    query {
      products(
        query: "status:active"
      ) {
        edges {
          node {
            __typename
            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 jsonl_string %}
      {"__typename":"Product","id":"gid://shopify/Product/1234567890","totalInventory":1}
      {"__typename":"Product","id":"gid://shopify/Product/2345678901","totalInventory":0}
    {% endcapture %}

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

  {% assign products = bulkOperation.objects | where: "__typename", "Product" %}

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

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

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

    {% else %}
      {% unless product.tags contains out_of_stock_tag %}
        {% assign tag_to_add = out_of_stock_tag %}
      {% endunless %}

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

    {% if tag_to_add != blank or tag_to_remove != blank %}
      {% action "shopify" %}
        mutation {
          {% if tag_to_add != blank %}
            tagsAdd(
              id: {{ product.id | json }}
              tags: {{ tag_to_add | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          {% endif %}
          {% if tag_to_remove != blank %}
            tagsRemove(
              id: {{ product.id | json }}
              tags: {{ tag_to_remove | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          {% endif %}
        }
      {% endaction %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic Liquid reference

Default option values

In stock tag
in-stock
Out of stock tag
out-of-stock
Monitor products for inventory updates
false