Auto-remove a product tag x days after it's added, with Mechanic.

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

Auto-remove a product tag x days after it's added

Use this this task to monitor for the addition of a new product tag, and to schedule the product to be untagged some number of days later. Useful for temporarily adding a product to a collection, or qualifying the product for some other temporary functionality.

Runs Occurs whenever a product is updated, or whenever a product is ordered, or whenever a variant is added, removed, or updated and Occurs whenever user/task/untag_product is triggered. Configuration includes tag to monitor and days to wait before untagging.

15-day free trial – unlimited tasks

Documentation

Use this this task to monitor for the addition of a new product tag, and to schedule the product to be untagged some number of days later. Useful for temporarily adding a product to a collection, or qualifying the product for some other temporary functionality.

This task monitors new and updated products, watching for the configured product tag.

As soon as that tag is detected, the task will add a second tag, indicating that the product is scheduled to be untagged. (For example, if the task is configured to watch for the tag "Approved", the task will add the tag "Approved - will be auto-removed by Mechanic".) The task will then schedule a followup event for the future, according to the configured number of days to wait. At that time, the task will remove both tags.

Important note: To prevent the task from untagging the product later, manually remove the task's additional tag (i.e. the "will be auto-removed by Mechanic" tag). If the additional tag is found missing, the task will leave the tag in place instead of auto-removing it.

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/products/update
user/task/untag_product
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
tag to monitor (required), days to wait before untagging (number, required)
Code
{% if event.topic == "user/task/untag_product" %}
  {% assign product_id = event.data.product_id %}
{% else %}
  {% assign product_id = product.admin_graphql_api_id %}
{% endif %}

{% assign tag = options.tag_to_monitor__required %}
{% assign untag_flag_tag = tag | append: " - will be auto-removed by Mechanic" %}
{% assign tag_removal_interval_s = options.days_to_wait_before_untagging__number_required | times: 24 | times: 60 | times: 60 | round %}
{% assign now_s = "now" | date: "%s" | times: 1 %}
{% assign metafield_key = task.id | sha256 | slice: 0, 7 %}

{% capture query %}
  query {
    product(id: {{ product_id | json }}) {
      id
      tags
      metafield(
        namespace: "mechanic"
        key: {{ metafield_key | json }}
      ) {
        id
        value
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "product": {
          "id": "gid://shopify/Product/1234567890",
          "tags": [{{ tag | json }}],
          "metafield": null
        }
      }
    }
  {% endcapture %}

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

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

{% if product.tags contains tag %}
  {% assign time_to_remove_s = product.metafield.value | times: 1 %}

  {% if product.metafield == nil or time_to_remove_s == 0 %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ product.id | json }}
          tags: {{ untag_flag_tag | json }}
        ) {
          node {
            ... on Product {
              tags
            }
          }
          userErrors {
            field
            message
          }
        }

        productUpdate(
          input: {
            id: {{ product.id | json }}
            metafields: [
              {
                namespace: "mechanic"
                key: {{ metafield_key | json }}
                value: {{ now_s | plus: tag_removal_interval_s | append: "" | json }}
                type: "number_integer"
              }
            ]
          }
        ) {
          product {
            metafield(
              namespace: "mechanic"
              key: {{ metafield_key | json }}
            ) {
              id
            }
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}

    {% action "event" %}
      {
        "topic": "user/task/untag_product",
        "data": {
          "product_id": {{ product.id | json }}
        },
        "run_at": {{ now_s | plus: tag_removal_interval_s | json }},
        "task_id": {{ task.id | json }}
      }
    {% endaction %}
  {% elsif product.tags contains untag_flag_tag %}
    {% if now_s < time_to_remove_s %}
      {% log message: "This product is scheduled to be untagged, but it's not time yet. Skipping.", tag: tag, untag_flag_tag: untag_flag_tag, now_s: now_s, time_to_remove_s: time_to_remove_s %}
    {% else %}
      {% log message: "This product is scheduled to be untagged, and that time is now.", tag: tag, untag_flag_tag: untag_flag_tag, now_s: now_s, time_to_remove_s: time_to_remove_s %}
      {% action "shopify" %}
        mutation {
          tagsRemove(
            id: {{ product.id | json }}
            tags: [{{ tag | json }}, {{ untag_flag_tag | json }}]
          ) {
            node {
              ... on Product {
                tags
              }
            }
            userErrors {
              field
              message
            }
          }

          metafieldDelete(
            input: {
              id: {{ product.metafield.id | json }}
            }
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% else %}
    {% log message: "This product has a tag removal time recorded, but the 'untag flag tag' has been removed. Skipping.", tag: tag, untag_flag_tag: untag_flag_tag, now_s: now_s, time_to_remove_s: time_to_remove_s %}
  {% endif %}
{% elsif event.topic == "user/task/untag_product" %}
  {% log message: "The tag auto-removal event has arrived, but the product has already been untagged by someone/something else. Skipping.", tag: tag, product_tags: product.tags %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more