Schedule product tags by date, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Schedule product tags by date

by Isaac Bowen (team@usemechanic.com)

Use this task to schedule adding and/or removing tags for specific products, with support for scheduling by date, or by date and hour.

Runs every day at midnight and when a user triggers the task. Configuration includes dates and tags to add, dates and tags to remove, product ids, use hour specificity, and test mode.

15-day free trial – unlimited tasks

Documentation

Use this task to schedule adding and/or removing product tags by date.

Use the "Product IDs" option to choose which products this task will tag. Learn how to find product IDs here.

Fill in the "Dates and tags to add" and/or "Dates and tags to remove" options with dates (including year, month, and day) on the left, and product tags on the right.

Enable "Use hour specificity" to have Mechanic schedule tags by date and hour, instead of only by date. When this option is enabled, fill in tag dates using an hour at the end, e.g. "2019-01-01 2PM". A date without an hour will be interpreted as midnight at the beginning of that date - for example, "2019-01-01" will be interpreted as "2019-01-01 12AM".

When "Test mode" is enabled, use the "Run task" button to see a summary of Mechanic's understanding of your configuration. Mechanic will not perform any tagging or untagging when this mode is enabled.

This task will run nightly, at midnight in your store's local timezone. If "Use hour specificity" is enabled, the task will run hourly instead. You can also run this task manually, using the "Run task" button, and the task will perform the operations appropriate for the current day (or hour, if "Use hour specificity" is enabled).

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
every day at midnight (mechanic/scheduler/daily)
when a user triggers the task (mechanic/user/trigger)
Options
dates and tags to add (keyval), dates and tags to remove (keyval), product ids (array, number, required), use hour specificity (boolean), test mode (boolean)
Script
{% if options.dates_and_tags_to_add__keyval == blank and options.dates_and_tags_to_remove__keyval == blank %}
  {"error": "Fill in at least one of 'Dates and tags to add' and 'Dates and tags to remove'. :)"}
{% endif %}

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

{% for product_id in options.product_ids__array_number_required %}
  {% assign products[products.size] = shop.products[product_id] %}
{% endfor %}

{% if options.use_hour_specificity__boolean %}
  {% assign time_format = "%Y-%m-%d %I%p" %}
{% else %}
  {% assign time_format = "%Y-%m-%d" %}
{% endif %}

{% assign now_time = "now" | date: time_format %}

{% assign test_mode = options.test_mode__boolean %}
{% assign full_schedule = hash %}

{% for keyval in options.dates_and_tags_to_add__keyval %}
  {% assign some_time = keyval[0] | date: time_format %}

  {% if some_time == now_time %}
    {% assign tags_to_add[tags_to_add.size] = keyval[1] %}
  {% endif %}

  {% if full_schedule[some_time] == nil %}
    {% assign full_schedule[some_time] = array %}
  {% endif %}

  {% assign n = full_schedule[some_time].size %}
  {% assign full_schedule[some_time][n] = "add: " | append: keyval[1] %}
{% endfor %}

{% for keyval in options.dates_and_tags_to_remove__keyval %}
  {% assign some_time = keyval[0] | date: time_format %}

  {% if some_time == now_time %}
    {% assign tags_to_remove[tags_to_remove.size] = keyval[1] %}
  {% endif %}

  {% if full_schedule[some_time] == nil %}
    {% assign full_schedule[some_time] = array %}
  {% endif %}

  {% assign n = full_schedule[some_time].size %}
  {% assign full_schedule[some_time][n] = "remove: " | append: keyval[1] %}
{% endfor %}

{% capture log %}
  {
    "now": {{ now_time | json }},
    "schedule": {{ full_schedule | json }},
    "product_ids": {{ options.product_ids__array_number_required | json }},
    "product_handles": {{ products | map: "handle" | json }}
  }
{% endcapture %}

{% if test_mode %}
  {% action "echo" %}{{ log }}{% endaction %}
{% else %}
  {"log": {{ log }}}
{% endif %}

{% if event.preview %}
  {% if options.dates_and_tags_to_add__keyval != blank %}
    {% assign tags_to_add = array %}
    {% assign tags_to_add[0] = options.dates_and_tags_to_add__keyval.first[1] %}
  {% endif %}

  {% if options.dates_and_tags_to_remove__keyval != blank %}
    {% assign tags_to_remove = array %}
    {% assign tags_to_remove[0] = options.dates_and_tags_to_remove__keyval.first[1] %}
  {% endif %}

  {% assign products = array %}
  {% assign products[0] = '{"admin_graphql_api_id": "gid://shopify/Product/1234567890"}' | parse_json %}
{% endif %}

{% unless test_mode %}
  {% if products != empty %}
    {% if tags_to_add != empty or tags_to_remove != empty %}
      {% for product in products %}
        {% action "shopify" %}
          mutation {
            {% if tags_to_add != empty %}
              tagsAdd(
                id: {{ product.admin_graphql_api_id | json }}
                tags: {{ tags_to_add | json }}
              ) {
                userErrors {
                  field
                  message
                }
              }
            {% endif %}

            {% if tags_to_remove != empty %}
              tagsRemove(
                id: {{ product.admin_graphql_api_id | json }}
                tags: {{ tags_to_remove | json }}
              ) {
                userErrors {
                  field
                  message
                }
              }
            {% endif %}
          }
        {% endaction %}
      {% endfor %}
    {% endif %}
  {% endif %}
{% endunless %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Dates and tags to add
{"2019-08-24"=>"saturday", "2019-08-23"=>"friday"}
Dates and tags to remove
{"2019-08-24"=>"friday", "2019-08-23"=>"thursday"}
Product ids
["1234567890"]