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.
Try this taskWhen it runs
- Occurs when a user manually triggers the task
- Occurs whenever a product is created
- Occurs whenever a product is updated, ordered, or variants are added, removed or updated
What you choose
- Date format
- Tag prefix
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. :)
Task code and subscriptions
You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.
Subscriptions
mechanic/user/trigger
shopify/products/create
shopify/products/update
How task subscriptions work
Option types and flags
- Date format (required)
- Tag prefix (required)
Task 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 %}
Mechanic Liquid reference
Default option values
- Date format
%B %Y- Tag prefix
Published: