Mechanic Get Mechanic
Task library

Auto-tag orders with chargeback or inquiry activity

Stay on top of chargebacks and inquiries! Nightly or on-demand, this task will scan the last 60 days of your order records, and apply the tags of your choice to orders with chargeback or inquiry activity, based on what that activity looks like.

Try this task

When it runs

  • Occurs when a user manually triggers the task
  • Occurs every day at midnight (in local time)

What you choose

  • Tag for any
  • Tag for accepted
  • Tag for charge refunded
Show 4 more
  • Tag for lost
  • Tag for needs response
  • Tag for under review
  • Tag for won

Documentation

Stay on top of chargebacks and inquiries! Nightly or on-demand, this task will scan the last 60 days of your order records, and apply the tags of your choice to orders with chargeback or inquiry activity, based on what that activity looks like.

If you would like for the task to scan orders beyond 60 days, you can enable read all orders in your Mechanic settings.

Learn more about chargebacks in the Shopify documentation:

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

mechanic/user/trigger
mechanic/scheduler/daily
How task subscriptions work

Option types and flags

Task code

{% assign tag_for_any = options.tag_for_any__required | strip  %}

{% assign statuses_and_tags = hash %}
{% assign statuses_and_tags["ACCEPTED"] = options.tag_for_accepted__required | strip %}
{% assign statuses_and_tags["CHARGE_REFUNDED"] = options.tag_for_charge_refunded__required | strip %}
{% assign statuses_and_tags["LOST"] = options.tag_for_lost__required | strip %}
{% assign statuses_and_tags["NEEDS_RESPONSE"] = options.tag_for_needs_response__required | strip %}
{% assign statuses_and_tags["UNDER_REVIEW"] = options.tag_for_under_review__required | strip %}
{% assign statuses_and_tags["WON"] = options.tag_for_won__required | strip %}

{% assign status_tags = statuses_and_tags | values %}

{% assign cursor = nil %}

{% for n in (0..100) %}
  {% capture query %}
    query {
      orders (
        first: 250
        after: {{ cursor | json }}
        query:"chargeback_status:any"
      ) {
        pageInfo {
          hasNextPage
        }
        edges {
          cursor
          node {
            id
            disputes {
              initiatedAs
              status
            }
            tags
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "orders": {
            "edges": [
              {
                "node": {
                  "id": "gid://shopify/Order/1234567890",
                  "legacyResourceId": "1234567890",
                  "disputes": [
                    {
                      "initiatedAs": "CHARGEBACK",
                      "status": "NEEDS_RESPONSE"
                    }
                  ]
                }
              }
            ]
          }
        }
      }
    {% endcapture %}

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

  {% assign orders = result.data.orders.edges | map: "node"  %}

  {% for order in orders %}
    {% assign tags_should_have = array %}
    {% assign tags_to_add = array %}
    {% assign tags_to_remove = array %}

    {% assign tags_should_have = tags_should_have | push: tag_for_any %}

    {% assign dispute_statuses
      = order.disputes
      | where: "initiatedAs", "CHARGEBACK"
      | map: "status"
    %}

    {% for dispute_status in dispute_statuses %}
      {% assign tags_should_have = tags_should_have | push: statuses_and_tags[dispute_status] %}
    {% endfor %}

    {% for status_tag in status_tags %}
      {% unless tags_should_have contains status_tag %}
        {% if order.tags contains status_tag %}
          {% assign tags_to_remove = tags_to_remove | push: status_tag %}
        {% endif %}
      {% endunless %}
    {% endfor %}

    {% for tag_should_have in tags_should_have %}
      {% unless order.tags contains tag_should_have %}
        {% assign tags_to_add = tags_to_add | push: tag_should_have %}
      {% endunless %}
    {% endfor %}

    {% log
      order_id: order.legacyResourceId,
      dispute_statuses: dispute_statuses,
      order_tags: order.tags,
      tags_should_have: tags_should_have,
      tags_to_add: tags_to_add,
      tags_to_remove: tags_to_remove
    %}

    {% if tags_to_add != blank or tags_to_remove != blank %}
      {% action "shopify" %}
        mutation {
          {% if tags_to_add != blank %}
            tagsAdd(
              id: {{ order.id | json }}
              tags: {{ tags_to_add | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          {% endif %}
          {% if tags_to_remove != blank %}
            tagsRemove(
              id: {{ order.id | json }}
              tags: {{ tags_to_remove | json }}
            ) {
              userErrors {
                field
                message
              }
            }
          {% endif %}
        }
      {% endaction %}
    {% endif %}
  {% endfor %}

  {% if result.data.orders.pageInfo.hasNextPage %}
    {% assign cursor = result.data.orders.edges.last.cursor %}
  {% else %}
    {% break %}
  {% endif %}
{% endfor %}
Mechanic Liquid reference

Default option values

Tag for any
chargeback-any
Tag for accepted
chargeback-accepted
Tag for charge refunded
chargeback-refunded
Tag for lost
chargeback-lost
Tag for needs response
chargeback-needs-response
Tag for under review
chargeback-under-review
Tag for won
chargeback-won