Set product templates based on product tags
Use this task to automatically select a specific product template for each of your products, based on how each product is tagged. Useful for a set-it-and-forget-it setup. :)
Try this taskWhen it runs
- Occurs when a user manually triggers the task
- Occurs when a bulk operation is completed
- Occurs whenever a product is created
- Occurs whenever a product is updated, ordered, or variants are added, removed or updated
What you choose
Product tags and template suffixes
Documentation
Use this task to automatically select a specific product template for each of your products, based on how each product is tagged. Useful for a set-it-and-forget-it setup. :)
To use this task, fill in "Product tags and template suffixes" with a single product tag on the left, per line, and a product template suffix on the right. For example, if you'd like products tagged "sale" to be shown with the products.sale-hero.liquid template, add an item that has "sale" on the left and "sale-hero" on the right.
If a product qualifies for several template suffixes based on its tags and on the task configuration, then this task will go with whichever template suffix match is found first.
To apply this task to your entire catalog, use the "Run task" button. Otherwise, this task will run automatically whenever a product is created or updated.
Note: If you want to reset a product to the default product template, then leave the template suffix empty for the associated tag.
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
mechanic/shopify/bulk_operation
shopify/products/create
shopify/products/update
How task subscriptions work
Option types and flags
- Product tags and template suffixes (keyval, required)
Task code
{% assign product_tags_and_template_suffixes = options.product_tags_and_template_suffixes__keyval_required %}
{% assign products = array %}
{% if event.topic == "mechanic/user/trigger" %}
{% capture bulk_operation_query %}
query {
products {
edges {
node {
__typename
id
tags
templateSuffix
}
}
}
}
{% endcapture %}
{% action "shopify" %}
mutation {
bulkOperationRunQuery(
query: {{ bulk_operation_query | json }}
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}
{% endaction %}
{% elsif event.topic contains "shopify/products/" or event.preview %}
{% capture query %}
query {
product(id: {{ product.admin_graphql_api_id | json }} ) {
id
tags
templateSuffix
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% assign products[0] = result.data.product %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
{% assign products = bulkOperation.objects %}
{% endif %}
{% if event.preview %}
{% capture products_json %}
[
{
"id": "gid://shopify/Product/4354268561469",
"tags": ["123", {{ product_tags_and_template_suffixes.first.first | json }}],
"templateSuffix": "preview-1234567890"
}
]
{% endcapture %}
{% assign products = products_json | parse_json %}
{% endif %}
{% for product in products %}
{% assign product_template_suffix_to_apply = nil %}
{% for keyval in product_tags_and_template_suffixes %}
{% assign product_tag_to_check = keyval[0] %}
{% assign product_template_to_apply = keyval[1] %}
{% if product.tags contains product_tag_to_check %}
{% assign product_template_suffix_to_apply = product_template_to_apply %}
{% break %}
{% endif %}
{% endfor %}
{% if product_template_suffix_to_apply != product.templateSuffix %}
{% action "shopify" %}
mutation {
productUpdate(
product: {
id: {{ product.id | json }}
templateSuffix: {{ product_template_suffix_to_apply | json }}
}
) {
product {
id
templateSuffix
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}
Mechanic Liquid reference