Mechanic is a development and ecommerce automation platform for Shopify. :)
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.
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. :)
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?)
mechanic/user/trigger shopify/products/create shopify/products/update
{% 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 %}
%B %Y
Published: