Auto-tag products based on their product type, with Mechanic.

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

Auto-tag products based on their product type

This task monitors new and updated products, applying the tag(s) of your choice to them based on their individual product type. Run this task manually to tag all of your products at once.

Runs Occurs whenever a product is created, Occurs whenever a product is updated, ordered, or variants are added, removed or updated, and Occurs when a user manually triggers the task. Configuration includes product types and tags and remove product tags in this list when they do not apply.

15-day free trial – unlimited tasks

Documentation

This task monitors new and updated products, applying the tag(s) of your choice to them based on their individual product type. Run this task manually to tag all of your products at once.

Configure the "Product types and tags" option with product types on the left, and the desired comma-delimited product tags on the right. Enable the "Remove product tags in this list when they do not apply" option to have this task proactively remove tags when they no longer apply. (When this option is enabled, this task will only remove tags that are associated with other known product types.)

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
shopify/products/create
shopify/products/update
mechanic/user/trigger
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
product types and tags (required, keyval), remove product tags in this list when they do not apply (boolean)
Code
{% assign products = array %}

{% if event.topic contains "shopify/products/" %}
  {% if event.preview %}
    {% capture product_json %}
      {
        "admin_graphql_api_id": "gid://shopify/Product/1234567890",
        "tags": "",
        "product_type": {{ options.product_types_and_tags__required_keyval.first.first | json }}
      }
    {% endcapture %}

    {% assign product = product_json | parse_json %}
  {% endif %}

  {% assign product_node = hash %}
  {% assign product_node["id"] = product.admin_graphql_api_id %}
  {% assign product_node["tags"] = product.tags | split: ", " %}
  {% assign product_node["productType"] = product.product_type %}
  {% assign products[0] = product_node %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign cursor = nil %}
  {% assign total_inventory = 0 %}

  {% for n in (0..100) %}
    {% capture query %}
      query {
        products(
          first: 250
          after: {{ cursor | json }}
        ) {
          pageInfo {
            hasNextPage
          }
          edges {
            cursor
            node {
              id
              tags
              productType
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "products": {
              "edges": [
                {
                  "node": {
                    "id": "gid://shopify/Product/4354268561469",
                    "tags": [],
                    "productType": {{ options.product_types_and_tags__required_keyval.first.first | json }}
                  }
                }
              ]
            }
          }
        }
      {% endcapture %}

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

    {% for product_edge in result.data.products.edges %}
      {% assign product_node = product_edge.node %}
      {% assign products[products.size] = product_node %}
    {% endfor %}

    {% if result.data.products.pageInfo.hasNextPage %}
      {% assign cursor = result.data.products.edges.last.cursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}
{% endif %}

{% for product in products %}
  {% assign deserved_tags = array %}
  {% assign tags_to_add = array %}
  {% assign tags_to_remove = array %}

  {% assign tags_for_product_type = options.product_types_and_tags__required_keyval[product.productType] | split: "," %}
  {% for tag in tags_for_product_type %}
    {% assign stripped_tag = tag | strip %}
    {% assign deserved_tags[deserved_tags.size] = stripped_tag %}
    {% unless product.tags contains stripped_tag or tags_to_add contains stripped_tag %}
      {% assign tags_to_add[tags_to_add.size] = stripped_tag %}
    {% endunless %}
  {% endfor %}

  {% if options.remove_product_tags_in_this_list_when_they_do_not_apply__boolean %}
    {% for pair in options.product_types_and_tags__required_keyval %}
      {% assign a_product_type = pair[0] %}
      {% assign tags_for_a_product_type = pair[1] | split: "," %}

      {% for tag in tags_for_a_product_type %}
        {% assign stripped_tag = tag | strip %}
        {% if product.tags contains stripped_tag %}
          {% unless deserved_tags contains stripped_tag or tags_to_remove contains stripped_tag %}
            {% assign tags_to_remove[tags_to_remove.size] = stripped_tag %}
          {% endunless %}
        {% endif %}
      {% endfor %}
    {% endfor %}
  {% endif %}

  {% if tags_to_add != empty or tags_to_remove != empty %}
    {% log product: product, tags_to_add: tags_to_add, tags_to_remove: tags_to_remove %}

    {% action "shopify" %}
      mutation {
        {% if tags_to_add != empty %}
          tagsAdd(
            id: {{ product.id | json }}
            tags: {{ tags_to_add | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        {% endif %}
        {% if tags_to_remove != empty %}
          tagsRemove(
            id: {{ product.id | json }}
            tags: {{ tags_to_remove | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        {% endif %}
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more