Archive orders when tagged, with Mechanic.

Mechanic is a development platform for Shopify. :)

Archive orders when tagged

by Isaac Bowen (team@usemechanic.com)

Add a tag, have the order automatically archived/closed. Simple. :)

Runs when an order is created, when an order is updated, and when a user triggers the task. Configuration includes required tag and allow manual unarchiving of orders.

15-day free trial – unlimited tasks

Documentation

This task monitors for new and updated orders, watching for the tag of your choice to be added. When the tag is found, this task will make sure the order is archived/closed.

Run this task manually to scan your older orders, archiving any that match your tag.

Enable "Allow manually unarchiving of orders" to have this task add a metafield to each order, as it archives them. The task will use this metafield as a reminder to not archive those orders in the future, regardless of what happens to them.

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 an order is updated (shopify/orders/updated)
when a user triggers the task (mechanic/user/trigger)
Options
required tag (required), allow manual unarchiving of orders (boolean)
Script
{% assign metafield_key = task.id | sha256 | slice: 0, 5 | prepend: "archived-" %}

{% assign orders = array %}
{% if event.preview or event.topic contains "shopify/orders/" %}
  {% assign orders[0] = order %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign orders = shop.orders.open %}
{% endif %}

{% for order in orders %}
  {% assign order_tags = order.tags | downcase | split: ", " %}
  {% assign tag_to_match = options.required_tag__required | downcase | strip %}

  {% assign order_qualifies = false %}
  {% if order.closed_at == blank and order_tags contains tag_to_match %}
    {% assign order_qualifies = true %}
  {% endif %}

  {% if options.allow_manual_unarchiving_of_orders__boolean %}
    {% if order.metafields.mechanic[metafield_key] %}
      {% assign order_qualifies = false %}
      {% log "Order has previously been archived, and manual unarchiving is allowed. Skipping." %}
    {% endif %}
  {% endif %}

  {% if event.preview or order_qualifies %}
    {% action "shopify" %}
      mutation {
        orderClose(
          input: {
            id: {{ order.admin_graphql_api_id | json }}
          }
        ) {
          order {
            closed
            closedAt
          }
          userErrors {
            field
            message
          }
        }
        
        {% if options.allow_manual_unarchiving_of_orders__boolean %}
          orderUpdate(
            input: {
              id: {{ order.admin_graphql_api_id | json }}
              metafields: [
                {
                  namespace: "mechanic"
                  key: {{ metafield_key | json }}
                  valueType: STRING
                  value: {{ "now" | date: "%FT%T%:z" | json }}
                }
              ]
            }
          ) {
            order {
              metafield(
                namespace: "mechanic"
                key: {{ metafield_key | json }}
              ) {
                id
                value
              }
            }
            userErrors {
              field
              message
            }
          }
        {% endif %}
      }
    {% endaction %}
  {% endif %}
{% endfor %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Required tag
archive-me