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

Mechanic is a development and ecommerce automation 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 Occurs whenever an order is created, Occurs whenever an order is updated, and Occurs when a user manually 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, 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 order tag (required), test mode (boolean)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Test mode
true