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.

Runs when an order is created and when a user triggers the task. Configuration includes required customer tag.

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

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)
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 %}
      {% capture mutation %}
        mutation {
          orderMarkAsPaid(input: {
            id: {{ order.admin_graphql_api_id | json }}
          }) {
            order {
              fullyPaid
            }
            userErrors {
              field
              message
            }
          }
        }
      {% endcapture %}

      {% action "shopify" mutation %}
    {% 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 {
                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 %}
          {% capture mutation %}
            mutation {
              orderMarkAsPaid(input: {
                id: {{ order_node.id | json }}
              }) {
                order {
                  fullyPaid
                }
                userErrors {
                  field
                  message
                }
              }
            }
          {% endcapture %}

          {% action "shopify" mutation %}
        {% 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