Auto-tag orders based on shipping method, with Mechanic.

Mechanic is an automation development platform 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 and allow partial matches for shipping method titles.

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".

Enable "Allow partial matches for shipping method titles" for more flexibility. With this option enabled, Mechanic will look for the first value in the "Shipping method titles and tags" list that is found within the order's shipping method title, and apply the corresponding tag.

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), allow partial matches for shipping method titles (boolean)
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/" %}
  {% if options.allow_partial_matches_for_shipping_method_titles__boolean %}
    {% for keyval in options.shipping_method_titles_and_tags__keyval_required %}
      {% if order.shipping_lines.first.title contains keyval[0] %}
        {% assign tag = keyval[1] %}
        {% break %}
      {% endif %}
    {% endfor %}
  {% else %}
    {% assign tag = options.shipping_method_titles_and_tags__keyval_required[order.shipping_lines.first.title] %}
  {% endif %}

  {% 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] = keyval[1] | json | prepend: "-tag:" %}
  {% endfor %}
  {% assign query = query_parts | uniq | 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 }}
          
          {% comment %}
            As of 2019-07-10, negative tag specifiers can result in an
            empty set of pages, *and* hasNextPage being true. Until that's
            fixed, we disable the query, and just deal with having to go
            through all orders. :)

            query: {{ query | json }}
          {% endcomment %}
        ) {
          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 = nil %}

      {% if options.allow_partial_matches_for_shipping_method_titles__boolean %}
        {% for keyval in options.shipping_method_titles_and_tags__keyval_required %}
          {% if order_node.shippingLine.title contains keyval[0] %}
            {% assign tag = keyval[1] %}
            {% break %}
          {% endif %}
        {% endfor %}
      {% else %}
        {% assign tag = options.shipping_method_titles_and_tags__keyval_required[order_node.shippingLine.title] %}
      {% endif %}

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