Auto-tag orders by discount code, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Auto-tag orders by discount code

by Isaac Bowen (team@usemechanic.com)

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

Runs when an order is created.

15-day free trial – unlimited tasks

Documentation

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

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)
Script
{% 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 tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.