Auto-tag orders with shipping label price adjustments, with Mechanic.

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

Auto-tag orders with shipping label price adjustments

This task will run daily to scan order timeline events from the past week to see if any orders have new shipping provider charges for labels. Qualifying orders will be tagged with the tag of your choice.

Runs Occurs every day at midnight (in local time) and Occurs when a user manually triggers the task. Configuration includes order tag to add.

15-day free trial – unlimited tasks

Documentation

This task will run daily to scan order timeline events from the past week to see if any orders have new shipping provider charges for labels. Qualifying orders will be tagged with the tag of your choice.

Run this task manually to scan the last 25K related order events. Note: You may need to enable read all orders to successfully tag qualifying orders older than 60 days.

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
mechanic/scheduler/daily
mechanic/user/trigger
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
order tag to add (required)
Code
{% assign order_tag_to_add = options.order_tag_to_add__required %}

{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {%- capture search_query -%}
    subject_type:ORDER action:shipping_label_adjustment_charge_created
  {%- endcapture -%}

  {% if event.topic contains "mechanic/scheduler/" %}
    {%- capture search_query -%}
      {{ search_query }} created_at:past_week
    {%- endcapture -%}
  {% endif %}

  {% comment %}
    -- get order timeline events related to shipping
    -- limit daily runs to a lookback of the past week; manual runs are only restricted by the number of events (25K max for this paginated query)
  {% endcomment %}

  {% assign cursor = nil %}
  {% assign events = array %}

  {% for n in (1..100) %}
    {% capture query %}
      query {
        events(
          first: 250
          reverse: true
          sortKey: CREATED_AT
          after: {{ cursor | json }}
          query: {{ search_query | json }}
        ) {
          pageInfo {
            hasNextPage
            endCursor
          }
          nodes {
            ... on BasicEvent {
              id
              action
              createdAt
              message
              subject {
                ... on Order {
                  id
                  name
                  createdAt
                  tags
                }
              }
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "events": {
              "nodes": [
                {
                  "id": "gid://shopify/BasicEvent/1234567890",
                  "action": "shipping_label_adjustment_charge_created",
                  "message": "You were charged $1.50 for a shipping label price adjustment.",
                  "subject": {
                    "id": "gid://shopify/Order/1234567890",
                    "name": "#PREVIEW"
                  }
                }
              ]
            }
          }
        }
      {% endcapture %}

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

    {% assign events = events | concat: result.data.events.nodes %}

    {% if result.data.events.pageInfo.hasNextPage %}
      {% assign cursor = result.data.events.pageInfo.endCursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}

  {% comment %}
    -- tag all orders associated with shipping label price adjustment events, unless they are already tagged
  {% endcomment %}

  {% for event in events %}
    {% assign order = event.subject %}

    {% if order == blank %}
      {% log
        message: "No order object was returned with this event. Check to make sure that Mechanic has access to order older than 60 days.",
        event: event
      %}
      {% continue %}
    {% endif %}

    {% unless order.tags contains order_tag_to_add %}
      {% log
        message: "Order qualified to be tagged",
        event: event
      %}

      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ order.id | json }}
            tags: {{ order_tag_to_add | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endunless %}
  {% endfor %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Order tag to add
shipping-label-price-adjustment