Auto-tag new products by "back in stock" age, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-tag new products by "back in stock" age

by Isaac Bowen (team@usemechanic.com)

This task monitors changes to inventory level, records the time at which a product's total inventory passes your configured threshold, and then maintains a set of tags indicating how recently the product returned to "back in stock" status.

Runs when an inventory level is updated, when a user triggers the task, and when a bulk operation finishes. Configuration includes back in stock inventory level, product tags and maximum age in days, run daily, and run hourly.

15-day free trial – unlimited tasks

Documentation

This task operates in two phases.

Phase one runs in response to inventory level changes. When a change brings a product's total inventory to at or above your configured threshold, a metafield will be added to your product, indicating when it returned to "back in stock" status. (Note: when this task encounters a product for the first time, and the product is already considered "back in stock", the task will record the current time.)

Phase two occurs when you run this task manually, or when it is run daily or hourly. In this phase, the task scans every product in your Shopify account, and updates tags in bulk, according to the task's configuration and according to the recorded "back in stock" time for each product.

Configure "Product tags and maximum age in days" with product tags on the left, and the maximum product age to consider on the right. For exampel, a product tag of "new-5" with a maximum age in days of "5" will be added to all products that were created/published within the last 5 days. Once a product ages beyond that threshold, this task will remove that tag during the next "phase two" run.

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 inventory level is updated (shopify/inventory_levels/update)
when a user triggers the task (mechanic/user/trigger)
when a bulk operation finishes (mechanic/shopify/bulk_operation)
Options
back in stock inventory level (number, required), product tags and maximum age in days (keyval, number, required), run daily (boolean), run hourly (boolean)
Script
{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler" %}
  {% capture bulk_operation_query %}
    query {
      products {
        edges {
          node {
            __typename
            id
            tags
            totalInventory
            metafield(
              namespace: "mechanic"
              key: "back_in_stock_s"
            ) {
              value
            }
          }
        }
      }
    }
  {% 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 %}
    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = array %}

    {% assign bulkOperation["objects"][0] = hash %}
    {% assign bulkOperation["objects"][0]["id"] = "gid://shopify/Product/1234567890" %}
    {% assign bulkOperation["objects"][0]["totalInventory"] = options.back_in_stock_inventory_level__number_required | plus: 10 %}
    {% assign bulkOperation["objects"][0]["tags"] = array %}
    {% assign bulkOperation["objects"][0]["metafield"] = hash %}
    {% assign bulkOperation["objects"][0]["metafield"]["value"] = "now" | date: "%s" | minus: 1000 %}

    {% assign bulkOperation["objects"][1] = hash %}
    {% assign bulkOperation["objects"][1]["id"] = "gid://shopify/Product/2345678901" %}
    {% assign bulkOperation["objects"][1]["totalInventory"] = options.back_in_stock_inventory_level__number_required | minus: 10 %}
    {% assign bulkOperation["objects"][1]["tags"] = array %}
    {% assign bulkOperation["objects"][1]["tags"][0] = options.product_tags_and_maximum_age_in_days__keyval_number_required.first.first %}
    {% assign bulkOperation["objects"][1]["metafield"] = nil %}
  {% endif %}

  {% for object in bulkOperation.objects %}
    {% assign product_timestamp_s = object.metafield.value | times: 1 %}

    {% assign tags_to_add = array %}
    {% assign tags_to_remove = array %}

    {% for keyval in options.product_tags_and_maximum_age_in_days__keyval_number_required %}
      {% assign tag = keyval[0] %}
      {% assign maximum_age_d = keyval[1] %}
      {% assign maximum_age_s = maximum_age_d | times: 24 | times: 60 | times: 60 %}

      {% assign threshold_timestamp_s = now_s | minus: maximum_age_s %}

      {% comment %}
        0 happens when we didn't have a valid datetime string to begin with. Happens
        when there's no published_at value.
      {% endcomment %}
      {% if product_timestamp_s != 0 and product_timestamp_s > threshold_timestamp_s %}
        {% unless object.tags contains tag %}
          {% assign tags_to_add[tags_to_add.size] = tag %}
        {% endunless %}
      {% else %}
        {% if object.tags contains tag %}
          {% assign tags_to_remove[tags_to_remove.size] = tag %}
        {% endif %}
      {% endif %}
    {% endfor %}

    {% if tags_to_add != empty or tags_to_remove != empty %}
      {% action "shopify" %}
        mutation {
          {% if tags_to_add != empty %}
            tagsAdd(
              id: {{ object.id | json }}
              tags: {{ tags_to_add | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          {% endif %}

          {% if tags_to_remove != empty %}
            tagsRemove(
              id: {{ object.id | json }}
              tags: {{ tags_to_remove | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          {% endif %}
        }
      {% endaction %}
    {% endif %}
  {% endfor %}
{% elsif event.topic contains "shopify/inventory_levels/" %}
  {% capture query %}
    query {
      inventoryLevel(id: {{ inventory_level.admin_graphql_api_id | json }}) {
        item {
          variant {
            product {
              id
              totalInventory
              metafield(
                namespace: "mechanic"
                key: "back_in_stock_s"
              ) {
                id
                value
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "inventoryLevel": {
            "item": {
              "variant": {
                "product": {
                  "id": "gid://shopify/Product/1234567890",
                  "totalInventory": {{ options.back_in_stock_inventory_level__number_required | plus: 1 }},
                  "metafield": null
                }
              }
            }
          }
        }
      }
    {% endcapture %}

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

  {% assign productNode = result.data.inventoryLevel.item.variant.product %}

  {% if productNode.metafield == nil and productNode.totalInventory >= options.back_in_stock_inventory_level__number_required %}
    {% action "shopify" %}
      mutation {
        productUpdate(
          input: {
            id: {{ productNode.id | json }}
            metafields: [
              {
                namespace: "mechanic"
                key: "back_in_stock_s"
                valueType: INTEGER
                value: {{ "now" | date: "%s" | json }}
              }
            ]
          }
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% elsif productNode.metafield != nil and productNode.totalInventory < options.back_in_stock_inventory_level__number_required %}
    {% action "shopify" %}
      mutation {
        metafieldDelete(
          input: {
            id: {{ productNode.metafield.id | json }}
          }
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Back in stock inventory level
50
Product tags and maximum age in days
{"new-1"=>"1", "new-5"=>"5", "new-9999"=>"9999"}