Mechanic Get Mechanic
Task library

Archive orders when tagged

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

Try this task

When it runs

  • Occurs whenever an order is created
  • Occurs whenever an order is updated
  • Occurs when a user manually triggers the task

What you choose

  • Required tag
  • Allow manual unarchiving of orders

Documentation

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

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.

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

shopify/orders/create
shopify/orders/updated
mechanic/user/trigger
How task subscriptions work

Option types and flags

Task code

{% 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 }}
                  type: "date_time"
                  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 Liquid reference

Default option values

Required tag
archive-me