Auto-tag orders based on shipping method with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Auto-tag orders based on shipping method

by Mechanic Team (team@usemechanic.com)

Quickly identify and sort orders based on their shipping method, using automatic order tags. This task supports auto-tagging incoming orders, and can be run manually to tag historical orders.

Runs when an order is created and when a user triggers the task. Configuration includes shipping method titles and tags.

15-day free trial – unlimited tasks

Documentation

To use this task, populate the "Shipping method titles and tags" list with shipping method titles on the left, and order tags to apply on the right. In Shopify, shipping method titles are configured as "rates"; you can find yours by navigating to Settings > Shipping, then looking at the rates within each shipping zone.

This task will run automatically on incoming orders. Use the "Run task" button to scan and tag older orders. To process orders older than 60 days, enable "Read all orders".

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)
when a user triggers the task (mechanic/user/trigger)
Options
shipping method titles and tags (keyval, required)
Script
{% if event.preview %}
  {% for keyval in options.shipping_method_titles_and_tags__keyval_required %}
    {% assign tag = keyval[1] %}
    {% break %}
  {% endfor %}

  {% capture mutation %}
    mutation {
      tagsAdd(
        id: "gid://shopify/Order/1234567890"
        tags: {{ tag | json }}
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endcapture %}

  {% action "shopify" mutation %}
{% elsif event.topic contains "shopify/orders/" %}
  {% assign tag = options.shipping_method_titles_and_tags__keyval_required[order.shipping_lines.first.title] %}
  {% assign order_tags = order.tags | split: ", " %}

  {"log": {{ order.shipping_lines.first.title | json | prepend: "Shipping method title for this order: " | json }}}

  {% if tag == nil %}
    {"log": "No tag to add for this order; nothing to do."}
  {% elsif order_tags contains tag %}
    {"log": "Order is already tagged {{ tag | json }}; nothing to do."}
  {% else %}
    {% capture mutation %}
      mutation {
        tagsAdd(
          id: {{ order.admin_graphql_api_id | default: "gid://shopify/Order/1234567890" | json }}
          tags: {{ tag | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endcapture %}

    {% action "shopify" mutation %}
  {% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign query_parts = array %}
  {% for keyval in options.shipping_method_titles_and_tags__keyval_required %}
    {% assign query_parts[query_parts.size] = "-tag:" | append: keyval[1] %}
  {% endfor %}
  {% assign query = query_parts | join: " AND " %}

  {"log": {{ query | json | prepend: "Searching for orders matching " | json }}}

  {% assign cursor = nil %}
  {% for n in (0..100) %}
    {% capture orders_query %}
      query {
        orders(
          first: 250
          after: {{ cursor | json }}
          query: {{ query | json }}
        ) {
          pageInfo {
            hasNextPage
          }
          edges {
            cursor
            node {
              id
              tags
              shippingLine {
                title
              }
            }
          }
        }
      }
    {% endcapture %}

    {% assign orders_result = orders_query | shopify %}

    {% for edge in orders_result.data.orders.edges %}
      {% assign order_node = edge.node %}
      {% assign tag = options.shipping_method_titles_and_tags__keyval_required[order_node.shippingLine.title] %}
      {% if tag %}
        {% unless order_node.tags contains tag %}
          {% capture mutation %}
            mutation {
              tagsAdd(
                id: {{ order_node.id | json }}
                tags: {{ tag | json }}
              ) {
                userErrors {
                  field
                  message
                }
              }
            }
          {% endcapture %}

          {% action "shopify" mutation %}
        {% endunless %}
      {% endif %}
    {% endfor %}

    {% if orders_result.data.orders.pageInfo.hasNextPage %}
      {% assign cursor = orders_result.data.orders.edges.last.cursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Shipping method titles and tags
{"Shipping Alpha"=>"alpha", "Shipping Beta"=>"beta", "Shipping Gamma"=>"gamma"}