Set product templates based on product tags, with Mechanic.

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

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. :)

Runs Occurs when a user manually triggers the task, Occurs when a bulk operation is completed, Occurs whenever a product is created, and Occurs whenever a product is updated, ordered, or variants are added, removed or updated. Configuration includes product tags and template suffixes.

15-day free trial – unlimited tasks

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.

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
mechanic/shopify/bulk_operation
shopify/products/create
shopify/products/update
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
product tags and template suffixes (keyval, required)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more