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

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

Auto-tag products by their publish date

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.

Runs Occurs when a user manually triggers the task, Occurs whenever a product is created, and Occurs whenever a product is updated, or whenever a product is ordered, or whenever a variant is added, removed, or 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, 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
mechanic/user/trigger
shopify/products/create
shopify/products/update
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
date format (required), tag prefix (required)
Code
{% if options.date_format__required contains "," %}
  {% error "Tags are not permitted to include commas (','). :)" %}
{% endif %}

{% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Date format
%B %Y
Tag prefix
Published: