Set product templates based on product tags, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Set product templates based on product tags

by Isaac Bowen (team@usemechanic.com)

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 when a user triggers the task, when a product is created, and when a product is updated. Configuration includes product tags and template suffixes.

15-day free trial – unlimited tasks

Documentation

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, Gurus, 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.

Events
when a user triggers the task (mechanic/user/trigger)
when a product is created (shopify/products/create)
when a product is updated (shopify/products/update)
Options
product tags and template suffixes (keyval, required)
Script
{% if event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        [
          "product",
          1234567890
        ],
        {
          "template_suffix": "special"
        }
      ]
    }
  }
{% elsif event.topic == "mechanic/user/trigger" %}
  {% for product in shop.products %}
    {% assign product_tags = product.tags | split: ", " %}
    {% 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 %}

    {% if product_template_suffix_to_apply == blank and product.template_suffix != blank %}
      {% assign product_qualifies_for_update = true %}
    {% elsif product_template_suffix_to_apply != product.template_suffix %}
      {% assign product_qualifies_for_update = true %}
    {% endif %}

    {% if product_qualifies_for_update %}
      {
        "action": {
          "type": "shopify",
          "options": [
            "update",
            [
              "product",
              {{ product.id | json }}
            ],
            {
              "template_suffix": {{ product_template_suffix_to_apply | json }}
            }
          ]
        }
      }
    {% endif %}
  {% endfor %}
{% elsif event.topic contains "shopify/products/" %}
  {% assign product_tags = product.tags | split: ", " %}
  {% 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 %}

  {% if product_template_suffix_to_apply == blank and product.template_suffix != blank %}
    {% assign product_qualifies_for_update = true %}
  {% elsif product_template_suffix_to_apply != product.template_suffix %}
    {% assign product_qualifies_for_update = true %}
  {% endif %}

  {% if product_qualifies_for_update %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "update",
          [
            "product",
            {{ product.id | json }}
          ],
          {
            "template_suffix": {{ product_template_suffix_to_apply | json }}
          }
        ]
      }
    }
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.