Maintain a tag for orders processed today, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Maintain a tag for orders processed today

by Isaac Bowen (team@usemechanic.com)

This task auto-tags today's new orders, as they're processed, and auto-untags them when the date changes. Optionally, choose to untag outside of a rolling 24-hour window, instead of untagging yesterday's orders at midnight.

Runs when an order is created and every day at midnight. Configuration includes use rolling 24 hour window and order tag.

15-day free trial – unlimited tasks

Documentation

This task auto-tags today's new orders, as they're processed, and auto-untags them when the date changes. Optionally, choose to untag outside of a rolling 24-hour window, instead of untagging yesterday's orders at midnight.

YouTube: See the development video for this task!

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)
every day at midnight (mechanic/scheduler/daily)
Options
use rolling 24 hour window (boolean), order tag (required)
Script
{% if options.use_rolling_24_hour_window__boolean %}
  {% assign now_s = "now" | date: "%s" | times: 1 %}
  {% assign twenty_four_hours_s = 24 | times: 60 | times: 60 %}
  {% assign threshold_s = now_s | minus: twenty_four_hours_s %}
{% else %}
  {% assign threshold_s = "now" | date: "%Y-%m-%d" | date: "%s" | times: 1 %}
{% endif %}

{% assign threshold_iso8601 = threshold_s | date: "%FT%T%:z" %}

{% assign order_nodes = array %}

{% if event.topic == "shopify/orders/create" %}
  {% if event.preview %}
    {% assign order = hash %}
    {% assign order["processed_at"] = threshold_s | plus: 1 | date: "%FT%T%:z" %}
    {% assign order["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
  {% endif %}

  {% assign order_node = hash %}
  {% assign order_node["id"] = order.admin_graphql_api_id %}
  {% assign order_node["processedAt"] = order.processed_at %}
  {% assign order_node["tags"] = order.tags | split: ", " %}

  {% assign order_nodes[order_nodes.size] = order_node %}
{% elsif event.topic contains "mechanic/scheduler/" %}
  {% assign cursor = nil %}

  {% for n in (0..100) %}
    {% capture query %}
      query {
        orders(
          first: 250
          after: {{ cursor | json }}
          query: {{ options.order_tag__required | json | prepend: "tag:" | json }}
        ) {
          pageInfo {
            hasNextPage
          }
          edges {
            cursor
            node {
              id
              processedAt
              tags
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "orders": {
              "edges": [
                {
                  "node": {
                    "id": "gid://shopify/Order/1234567890",
                    "processedAt": "2000-01-01T00:00:00Z",
                    "tags": [
                      {{ options.order_tag__required | json }}
                    ]
                  }
                }
              ]
            }
          }
        }
      {% endcapture %}

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

    {% for order_edge in result.data.orders.edges %}
      {% assign order_nodes[order_nodes.size] = order_edge.node %}
    {% endfor %}

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

{% for order_node in order_nodes %}
  {% assign processed_at_s = order_node.processedAt | date: "%s" | times: 1 %}
  {% if processed_at_s >= threshold_s %}
    {% unless order_node.tags contains options.order_tag__required %}
      {% action "shopify" %}
        mutation {
          tagsAdd(
            id: {{ order_node.id | json }}
            tags: {{ options.order_tag__required | json }}
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endunless %}
  {% elsif order_node.tags contains options.order_tag__required %}
    {% action "shopify" %}
      mutation {
        tagsRemove(
          id: {{ order_node.id | json }}
          tags: {{ options.order_tag__required | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.