Auto-tag products by their publish date, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-tag products by their publish date

by Isaac Bowen (team@usemechanic.com)

This task formats the publish date of each product (according to the format you choose), adds a prefix (also of your choosing), and applies it to each product.

Runs when a user triggers the task, when a product is created, and when a product is updated. Configuration includes date format and tag prefix.

15-day free trial – unlimited tasks

Documentation

This task formats the publish date of each product (according to the format you choose), adds a prefix (also of your choosing), and applies it to each product. Run this task manually to scan your entire product catalog at once, or wait for the task to run automatically when products are created or updated.

Use strfti.me to build a date format that suits you. Use the previews to the right of the task options form to verify that your date format does what you expect. If you've got any questions, use the chat button in the corner. :)

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)
when a product is created (shopify/products/create)
when a product is updated (shopify/products/update)
Options
date format (required), tag prefix (required)
Script
{% if event.preview %}
  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: "gid://shopify/Product/1234576890"
        tags: {{ "now" | date: options.date_format__required | prepend: options.tag_prefix__required | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}

{% if event.topic == "mechanic/user/trigger" %}
  {% assign products = shop.products %}
{% elsif event.topic contains "shopify/products/" %}
  {% assign products = array %}
  {% assign products[0] = product %}
{% endif %}

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

  {% assign calculated_published_at_tag = nil %}
  {% assign new_published_at_tag = nil %}
  {% assign old_published_at_tags = array %}

  {% if product.published_at != blank %}
    {% assign calculated_published_at_tag = product.published_at | date: options.date_format__required | prepend: options.tag_prefix__required %}
  {% endif %}

  {% for tag in product_tags %}
    {% if tag == calculated_published_at_tag %}
      {% continue %}
    {% endif %}

    {% assign potential_tag_prefix = tag | slice: 0, options.tag_prefix__required.size %}
    {% if potential_tag_prefix == options.tag_prefix__required %}
      {% assign old_published_at_tags[old_published_at_tags.size] = tag %}
    {% endif %}
  {% endfor %}

  {% unless product_tags contains calculated_published_at_tag %}
    {% assign new_published_at_tag = calculated_published_at_tag %}
  {% endunless %}

  {% if new_published_at_tag != blank or old_published_at_tags != empty %}
    {% action "shopify" %}
      mutation {
        {% if new_published_at_tag != blank %}
          tagsAdd(
            id: {{ product.admin_graphql_api_id | json }}
            tags: {{ new_published_at_tag | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        {% endif %}

        {% if old_published_at_tags != empty %}
          tagsRemove(
            id: {{ product.admin_graphql_api_id | json }}
            tags: {{ old_published_at_tags | 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
Date format
%B %Y
Tag prefix
Published: