Auto-tag orders using product tags, with Mechanic.

Mechanic is a development platform for Shopify. :)

Auto-tag orders using product tags

by Isaac Bowen (team@usemechanic.com)

Use this task to tag incoming orders with all the product tags in the order. Optionally, specify a specific list of tags to be copied, and/or a certain tag prefix to watch for.

Runs when an order is created. Configuration includes only copy these tags and only copy tags having this prefix.

15-day free trial – unlimited tasks

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 an order is created (shopify/orders/create)
Options
only copy these tags (array), only copy tags having this prefix
Script
{% if event.preview %}
  {% capture order_json %}
    {
      "admin_graphql_api_id": "gid://shopify/Order/12345",
      "line_items": [
        {
          "product": {
            "tags": {{ options.only_copy_these_tags__array | join: ", " | default: "preorder" | json }}
          }
        },
        {
          "product": {
            "tags": {{ options.only_copy_tags_having_this_prefix | default: "important" | append: "-order" | json }}
          }
        }
      ]
    }
  {% endcapture %}

  {% assign order = order_json | parse_json %}
{% endif %}

{% assign order_product_tags = order.line_items | map: "product" | map: "tags" | join: ", " | split: ", " %}
{% assign existing_order_tags = order.tags | split: ", " %}
{% assign tags_to_add = array %}

{% log order_product_tags: order_product_tags, existing_order_tags: existing_order_tags %}

{% for tag in order_product_tags %}
  {% if existing_order_tags contains tag %}
    {% continue %}
  {% endif %}

  {% if options.only_copy_these_tags__array != blank %}
    {% if options.only_copy_these_tags__array contains nil %}
      {% error "'Only copy these tags' must not contain any blank items. If you don't want to use this option, remove its remaining items." %}
    {% endif %}

    {% unless options.only_copy_these_tags__array contains tag %}
      {% continue %}
    {% endunless %}
  {% endif %}

  {% if options.only_copy_tags_having_this_prefix != blank %}
    {% assign prefix_length = options.only_copy_tags_having_this_prefix.size %}
    {% assign tag_substr = tag | slice: 0, prefix_length %}
    {% if tag_substr != options.only_copy_tags_having_this_prefix %}
      {% continue %}
    {% endif %}
  {% endif %}

  {% assign tags_to_add[tags_to_add.size] = tag %}
{% endfor %}

{% if event.preview and tags_to_add == empty %}
  {% error "It looks like you have conflicting options configured. Please double-check your options, and try again. :)" %}
{% endif %}

{% if tags_to_add != empty %}
  {% action "shopify" %}
    mutation {
      tagsAdd(
        id: {{ order.admin_graphql_api_id | json }}
        tags: {{ tags_to_add | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.