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, or whenever a product is ordered, or whenever a variant is 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, this task will go with whichever tmeplate 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.

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 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 %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "product": {
              "id": "gid://shopify/Product/4354268561469",
              "tags": ["123", {{ options.product_tags_and_template_suffixes__keyval_required.first.first | json }}],
              "template_suffix": {{ options.product_tags_and_template_suffixes__keyval_required.first.last | json }}
            }
          }
        }
      {% endcapture %}

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

  {% assign products[0] = result.data.product %}

{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% assign products = bulkOperation.objects %}
{% endif %}

{% for product in products %}
  {% assign product_qualifies_for_update = false %}
  {% assign product_template_suffix_to_apply = nil %}

  {% for keyval in options.product_tags_and_template_suffixes__keyval_required %}
    {% 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 %}

  {% comment %} Allow setting to template to blank{% endcomment %}
  {% if product_template_suffix_to_apply == blank and product.templateSuffix != blank %}
    {% assign product_qualifies_for_update = true %}
  {% comment %} Make sure the template is not the right one already {% endcomment %}  
  {% elsif product_template_suffix_to_apply != product.templateSuffix %}
    {% assign product_qualifies_for_update = true %}
  {% endif %}  

  {% if product_qualifies_for_update %}
    {% action "shopify" %}
      mutation {
        productUpdate(
          input: {
            id: {{ product.id | json }}
            templateSuffix: {{ product_template_suffix_to_apply | json }}
          }
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}  
  {% endif %}

{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more