Mechanic Get Mechanic
Task library

Auto-tag orders by discount code

This task watches for newly-created orders, and copies any discount codes present to the order's tags.

Try this task

When it runs

Occurs whenever an order is created

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
How task subscriptions work

Task code

{% if event.preview %}
  {% assign order = hash %}
  {% assign order["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
  {% assign order["discount_codes"] = array %}
  {% assign order["discount_codes"][0] = hash %}
  {% assign order["discount_codes"][0]["code"] = "ABC123" %}
{% endif %}

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