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 specific product tag, and to schedule the product to be untagged a configurable 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, ordered, or variants are 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 specific product tag, and to schedule the product to be untagged a configurable number of days later. Useful for temporarily adding a product to a collection, or qualifying the product for some other temporary functionality.

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).

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
{% assign tag_to_monitor = options.tag_to_monitor__required %}
{% assign tag_removal_interval_s = options.days_to_wait_before_untagging__number_required | times: 24 | times: 60 | times: 60 | round %}

{% assign untag_flag_tag = tag_to_monitor | append: " - will be auto-removed by Mechanic" %}
{% assign now_s = "now" | date: "%s" | times: 1 %}
{% assign metafield_key = task.id | sha256 | slice: 0, 7 %}

{% if event.topic == "user/task/untag_product" %}
  {% assign product_id = event.data.product_id %}
{% else %}
  {% assign product_id = product.admin_graphql_api_id %}
{% endif %}

{% 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_to_monitor | json }}
        }
      }
    }
  {% endcapture %}

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

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

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

  {% if product.metafield == blank 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
          }
        }

        metafieldsSet(
          metafields: [
            {
              ownerId: {{ product.id | json }}
              namespace: "mechanic"
              key: {{ metafield_key | json }}
              value: {{ now_s | plus: tag_removal_interval_s | append: "" | json }}
              type: "number_integer"
            }
          ]
        ) {
          metafields {
            id
            namespace
            key
            type
            value
            owner {
              ... on Product {
                id
                title
              }
            }
          }
          userErrors {
            code
            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_to_monitor: tag_to_monitor,
        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_to_monitor: tag_to_monitor,
        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: {{ array | push: tag_to_monitor, untag_flag_tag | json }}
          ) {
            node {
              ... on Product {
                tags
              }
            }
            userErrors {
              field
              message
            }
          }

          metafieldsDelete(
            metafields: [
              {
                ownerId: {{ product.id | json }}
                namespace: "mechanic"
                key: {{ metafield_key | json }}
              }
            ]
          ) {
            deletedMetafields {
              ownerId
              namespace
              key
            }
            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_to_monitor: tag_to_monitor,
      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_to_monitor: tag_to_monitor,
    product_tags: product.tags
  %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more