Archive orders when tagged, with Mechanic.

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

Archive orders when tagged

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

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

15-day free trial – unlimited tasks

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.

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
shopify/orders/create
shopify/orders/updated
mechanic/user/trigger
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
required tag (required), allow manual unarchiving of orders (boolean)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Required tag
archive-me