Mechanic Get Mechanic
Task library

Auto-pay orders from customers with a certain tag

Use this task to immediately bump orders, from certain customers, to "paid". Useful if you have a standing payment arrangement for certain customers. Optionally, auto-remove this tag from customers after auto-paying their order. Useful for using customer tags as one-time triggers for auto-payment.

Try this task

When it runs

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

What you choose

  • Required customer tag
  • Remove tag from customer after processing their order

Documentation

Use this task to immediately bump orders, from certain customers, to "paid". Useful if you have a standing payment arrangement for certain customers. Optionally, auto-remove this tag from customers after auto-paying their order. Useful for using customer tags as one-time triggers for auto-payment.

This task runs automatically for incoming orders. Run this task manually to scan your store's order history, processing orders that were previously placed. (To scan orders older than 60 days, enable "Read all orders".)

Enable the "Remove tag from customer after processing their order" option to have this task remove the required tag whenever this task marks an order of theirs as paid. (If run manually, this task will still mark all of the qualifying customer's orders as paid, not just one order.)

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
mechanic/user/trigger
How task subscriptions work

Option types and flags

Task code

{% assign tag_to_match = options.required_customer_tag__required | downcase %}

{% if event.topic contains "shopify/orders" %}
  {% assign customer_tags = order.customer.tags | downcase | split: ", " %}

  {% capture order_query %}
    query {
      order(id: {{ order.admin_graphql_api_id | json }}) {
        canMarkAsPaid
      }
    }
  {% endcapture %}

  {% assign order_result = order_query | shopify %}

  {% if event.preview or customer_tags contains tag_to_match %}
    {% if order_result.data.order.canMarkAsPaid == false %}
      {"log": "Order cannot be manually marked as paid - skipping."}
    {% else %}
      {% action "shopify" %}
        mutation {
          orderMarkAsPaid(input: {
            id: {{ order.admin_graphql_api_id | json }}
          }) {
            order {
              fullyPaid
            }
            userErrors {
              field
              message
            }
          }

          {% if options.remove_tag_from_customer_after_processing_their_order__boolean %}
            tagsRemove(
              id: {% if event.preview %}"gid://shopify/Customer/1234567890"{% else %}{{ order.customer.admin_graphql_api_id | json }}{% endif %}
              tags: {{ options.required_customer_tag__required | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          {% endif %}
        }
      {% endaction %}
    {% endif %}
  {% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign cursor = nil %}

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

    {% assign orders_result = orders_query | shopify %}

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

      {% if order_node.canMarkAsPaid %}
        {% assign customer_tags = order_node.customer.tags | join: ", " | downcase | split: ", " %}

        {% if customer_tags contains tag_to_match %}
          {% action "shopify" %}
            mutation {
              orderMarkAsPaid(input: {
                id: {{ order_node.id | json }}
              }) {
                order {
                  fullyPaid
                }
                userErrors {
                  field
                  message
                }
              }

              {% if options.remove_tag_from_customer_after_processing_their_order__boolean %}
                tagsRemove(
                  id: {% if event.preview %}"gid://shopify/Customer/1234567890"{% else %}{{ order_node.customer.id | json }}{% endif %}
                  tags: {{ options.required_customer_tag__required | json }}
                ) {
                  userErrors {
                    field
                    message
                  }
                }
              {% endif %}
            }
          {% endaction %}
        {% endif %}
      {% 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

Required customer tag
autopay