Mechanic Get Mechanic
Task library

Auto-tag new orders based on payment method

This task checks the payment gateways and payment methods on order creation and updates, and tags the order accordingly if any of the gateways or methods match a configured value.

Try this task

When it runs

  • Occurs whenever an order is created
  • Occurs whenever an order is updated, with a 15 second delay

What you choose

Payment gateway names and tags

Documentation

This task checks the payment gateways and payment methods on order creation and updates, and tags the order accordingly if any of the gateways or methods match a configured value.

Configure the "Payment gateway names and tags" fields with each payment gateway name you wish to monitor on the left, and the associated tag on the right.

Tip: this task will log out the payment gateways and methods from success transactions on each run, so review the log to verify any configuration values you wish to use (e.g. "gift_card", "paypal"). Case-sensitivity is important!

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
shopify/orders/updated+15.seconds
How task subscriptions work

Option types and flags

Task code

{% assign payment_gateway_names_and_tags = options.payment_gateway_names_and_tags__keyval_required %}

{% if event.topic == "shopify/orders/create" or event.topic == "shopify/orders/updated" %}
  {% comment %}
    -- get order and transactions data
  {% endcomment %}

  {% capture query %}
    query {
      order(
        id: {{ order.admin_graphql_api_id | json }}
      ) {
        id
        name
        displayFinancialStatus
        paymentGatewayNames
        tags
        transactions(first: 10) {
          id
          status
          kind
          formattedGateway
          gateway
          paymentDetails {
            __typename
            ... on BasePaymentDetails {
              paymentMethodName
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "order": {
            "id": "gid://shopify/Order/1234567890",
            "paymentGatewayNames": {{ array | push: payment_gateway_names_and_tags.first.first | json }}
          }
        }
      }
    {% endcapture %}

    {% assign result = result_json | parse_json %}
  {% endif %}

  {% assign order = result.data.order %}

  {% log order_data: order %}

  {% comment %}
    -- get all payment gateways and payment methods (from success transactions)
  {% endcomment %}

  {% assign payment_gateways_and_methods
    = order.transactions
    | where: "status", "SUCCESS"
    | default: array
    | map: "paymentDetails"
    | map: "paymentMethodName"
    | concat: order.paymentGatewayNames
    | uniq
  %}

  {% assign tags_to_add = array %}

  {% for keyval in payment_gateway_names_and_tags %}
    {% assign payment_gateway_name = keyval[0] %}
    {% assign tag = keyval[1] %}

    {% if payment_gateways_and_methods contains payment_gateway_name %}
      {% unless order.tags contains tag %}
        {% assign tags_to_add = tags_to_add | push: tag %}
      {% endunless %}
    {% endif %}
  {% endfor %}

  {% if tags_to_add != blank %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ order.id | json }}
          tags: {{ tags_to_add | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endif %}
Mechanic Liquid reference