Mechanic Get Mechanic
Task library

Auto-pay orders having a certain tag

Use this task to quickly designate certain orders to be automatically marked as paid. Runs for orders as they're created and updated, and can also be run manually, to scan all orders in your store at once.

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 order tag
  • Test mode

Documentation

Use this task to quickly designate certain orders to be automatically marked as paid. Runs for orders as they're created and updated, and can also be run manually, to scan all orders in your store at once.

When running manually, use test mode first to ensure that you'll receive the results you expect. :)

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

{% if event.topic contains "shopify/orders" %}
  {% capture query %}
    query {
      order(id: {{ order.admin_graphql_api_id | json }}) {
        tags
        canMarkAsPaid
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "order": {
            "tags": [
              {{ options.required_order_tag__required | json }}
            ],
            "canMarkAsPaid": true
          }
        }
      }
    {% endcapture %}

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

  {% assign order_node = result.data.order %}

  {% if order_node.tags contains options.required_order_tag__required %}
    {% if order_node.canMarkAsPaid == false %}
      {% log "Order cannot be manually marked as paid - skipping." %}
    {% elsif options.test_mode__boolean %}
      {% action "echo" "This order qualifies to be marked as paid." %}
    {% else %}
      {% action "shopify" %}
        mutation {
          orderMarkAsPaid(input: {
            id: {{ order.admin_graphql_api_id | json }}
          }) {
            order {
              fullyPaid
            }
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign cursor = nil %}

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

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "orders": {
              "edges": [
                {
                  "node": {
                    "id": "gid://shopify/Order/1234567890",
                    "name": "#1234",
                    "tags": [
                      {{ options.required_order_tag__required | json }}
                    ],
                    "canMarkAsPaid": true
                  }
                }
              ]
            }
          }
        }
      {% endcapture %}

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

    {% for edge in result.data.orders.edges %}
      {% assign order_node = edge.node %}

      {% if order_node.canMarkAsPaid == false %}
        {% continue %}
      {% endif %}

      {% unless order_node.tags contains options.required_order_tag__required %}
        {% continue %}
      {% endunless %}

      {% if options.test_mode__boolean %}
        {% assign message = "Order " | append: order_node.name | append: " qualifies to be marked as paid." %}
        {% action "echo" message %}
      {% else %}
        {% action "shopify" %}
          mutation {
            orderMarkAsPaid(input: {
              id: {{ order_node.id | json }}
            }) {
              order {
                name
                fullyPaid
              }
              userErrors {
                field
                message
              }
            }
          }
        {% endaction %}
      {% endif %}
    {% endfor %}

    {% if orders_result.data.orders.pageInfo.hasNextPage %}
      {% assign cursor = orders_result.data.orders.edges.last.cursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic Liquid reference

Default option values

Test mode
true