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 a product (according to the format you choose), adds a prefix (also of your choosing), and applies it as a tag to the product. Run this task manually to scan your entire catalog of active products, 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, ordered, or variants are 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 a product (according to the format you choose), adds a prefix (also of your choosing), and applies it as a tag to the product. Run this task manually to scan your entire catalog of active products, 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
{% assign date_format = options.date_format__required %}
{% assign tag_prefix = options.tag_prefix__required %}

{% if date_format contains "," %}
  {% error "Tags are not permitted to include commas (','). :)" %}
{% endif %}

{% if event.topic contains "shopify/products/" %}
  {% capture query %}
    query {
      product(id: {{ product.admin_graphql_api_id | json }}) {
        id
        publishedAt
        tags
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% assign products = array | push: result.data.product %}

{% elsif event.topic == "mechanic/user/trigger" %}
  {% comment %}
    -- get up to 25K active products in the shop
  {% endcomment %}

  {% assign cursor = nil %}
  {% assign products = array %}

  {% for n in (1..100) %}
    {% capture query %}
      query {
        products(
          first: 250
          after: {{ cursor | json }}
          query: "status:active"
        ) {
          pageInfo {
            hasNextPage
            endCursor
          }
          nodes {
            id
            publishedAt
            tags
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "products": {
              "nodes": [
                {
                  "id": "gid://shopify/Product/1234567890"
                }
              ]
            }
          }
        }
      {% endcapture %}

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

    {% assign products = products | concat: result.data.products.nodes %}

    {% if result.data.products.pageInfo.hasNextPage %}
      {% assign cursor = result.data.products.pageInfo.endCursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}
{% endif %}

{% if event.preview %}
  {% capture products_json %}
    [
      {
        "id": "gid://shopify/Product/1234567890",
        "publishedAt": {{ "now" | date: "%F" | json }},
        "tags": [
          {{ "now - 1 year" | date: date_format | prepend: tag_prefix | json }}
        ]
      }
    ]
  {% endcapture %}

  {% assign products = products_json | parse_json %}
{% endif %}

{% comment %}
  -- process all products from paginated query, or the single product that initiated the webhook event
{% endcomment %}

{% for product in products %}
  {% assign calculated_published_at_tag = nil %}
  {% assign new_published_at_tag = nil %}
  {% assign old_published_at_tags = array %}

  {% if product.publishedAt != blank %}
    {% assign calculated_published_at_tag = product.publishedAt | date: date_format | prepend: tag_prefix %}
  {% endif %}

  {% for tag in product.tags %}
    {% if tag == calculated_published_at_tag %}
      {% continue %}
    {% endif %}

    {% assign potential_tag_prefix = tag | slice: 0, tag_prefix.size %}

    {% if potential_tag_prefix == tag_prefix %}
      {% assign old_published_at_tags = old_published_at_tags | push: 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 != blank %}
    {% action "shopify" %}
      mutation {
        {% if new_published_at_tag != blank %}
          tagsAdd(
            id: {{ product.id | json }}
            tags: {{ new_published_at_tag | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        {% endif %}

        {% if old_published_at_tags != blank %}
          tagsRemove(
            id: {{ product.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: