Auto-tag new products by age with Mechanic.

Mechanic is an automation and development platform for Shopify. :)

Auto-tag new products by age

by Isaac Bowen (team@usemechanic.com)

This task scans every product in your store, and adds or removes the tags you configure, according to how many days have passed since the product was created. (Or published, if you prefer!)

Runs when a user triggers the task. Configuration includes use product publish times instead of creation times, product tags and maximum age in days, run daily, and run hourly.

15-day free trial – unlimited tasks

Documentation

This task scans every product in your store, and applies the tags you configure, according to the product's age.

Enable "Use product publish times instead of creation times" to have this task look at when the product was published to your online storefront, instead of when the product was created. When this option is enabled, unpublished products will have any of this task's tags removed.

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, running this task will remove that tag.

Run this task manually to scan your store's products on demand, or configure the task to run daily/hourly.

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 a user triggers the task (mechanic/user/trigger)
Options
use product publish times instead of creation times (boolean), product tags and maximum age in days (keyval, number, required), run daily (boolean), run hourly (boolean)
Script
{% if event.preview %}
  {% assign products = array %}
  {% assign products[0] = '{"admin_graphql_api_id":"gid://shopify/Product/1234567890","created_at":"1900-01-01","published_at":"2000-01-01"}' | parse_json %}
{% else %}
  {% assign products = shop.products %}
{% endif %}

{% assign now_s = "now" | date: "%s" | times: 1 %}

{% for product in products %}
  {% assign product_tags = product.tags | split: ", " %}

  {% assign product_timestamp_s = product.created_at | date: "%s" | times: 1 %}
  {% if options.use_product_publish_times_instead_of_creation_times__boolean %}
    {% assign product_timestamp_s = product.published_at | date: "%s" | times: 1 %}
  {% endif %}

  {% 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 product_tags contains tag %}
        {% assign tags_to_add[tags_to_add.size] = tag %}
      {% endunless %}
    {% else %}
      {% if product_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: {{ 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 %}
  {% endif %}
{% endfor %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Use product publish times instead of creation times
true
Product tags and maximum age in days
{"new-1"=>"1", "new-5"=>"5", "new-9999"=>"9999"}