Auto-pay orders having a certain tag, with Mechanic.

Mechanic is a development platform for Shopify. :)

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.

Runs when an order is created, when an order is updated, and when a user triggers the task. Configuration includes required order tag and test mode.

15-day free trial – unlimited tasks

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. :)

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.

Open source
View on GitHub to contribute to this task
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 order tag (required), test mode (boolean)
Script
{% 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 tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Test mode
true