Auto-pay orders from customers with a certain tag with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Auto-pay orders from customers with a certain tag

by Mechanic Team (team@usemechanic.com)

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.

Runs when an order is created and when a user triggers the task. Configuration includes required customer tag and remove tag from customer after processing their order.

15-day free trial – unlimited tasks

Documentation

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

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.

Events
when an order is created (shopify/orders/create)
when a user triggers the task (mechanic/user/trigger)
Options
required customer tag (required), remove tag from customer after processing their order (boolean)
Script
{% 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 tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Required customer tag
autopay