Mechanic Get Mechanic
Task library

Auto-copy customer metafields to new orders

When new orders are created, this task will check to see if the customer has any of the configured metafields, and if so it will copy the metafield values to the paired order attributes.

Try this task

When it runs

Occurs whenever an order is created

What you choose

Customer metafields and order attributes

Documentation

When new orders are created, this task will check to see if the customer has any of the configured metafields, and if so it will copy the metafield values to the paired order attributes.

Configure this task by adding customer metafields on the left, in the form of namespace.key, and the order attribute names on the right.

Example configuration: "custom.group": "Group"

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

Option types and flags

Task code

{% assign customer_metafields_and_order_attributes = options.customer_metafields_and_order_attributes__keyval_required %}

{% capture query %}
  query {
    order(id: {{ order.admin_graphql_api_id | json }}) {
      id
      customer {
        metafields(
          first: {{ customer_metafields_and_order_attributes.size }}
          keys: {{ customer_metafields_and_order_attributes | keys | json }}
        ) {
          nodes {
            key
            value
          }
        }
      }
      customAttributes {
        key
        value
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "order": {
          "id": "gid://shopify/Order/1234567890",
          "customer": {
            "metafields": {
              "nodes": [
                {
                  "key": {{ customer_metafields_and_order_attributes | first | first | json }},
                  "value": "Alpha"
                }
              ]
            }
          },
          "customAttributes": [
            {
              "key": "__sample",
              "value": "Beta"
            }
          ]
        }
      }
    }
  {% endcapture %}

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

{% assign metafields = result.data.order.customer.metafields.nodes %}
{% assign attributes = result.data.order.customAttributes | default: array %}

{% log
  existing_attributes_on_order: attributes,
  matched_customer_metafields: metafields
%}

{% for metafield in metafields %}
  {% assign attribute = hash %}
  {% assign attribute["key"] = customer_metafields_and_order_attributes[metafield.key] %}
  {% assign attribute["value"] = metafield.value %}
  {% assign attributes = attributes | push: attribute %}
{% else %}
  {% break %}
{% endfor %}

{% action "shopify" %}
  mutation {
    orderUpdate(
      input: {
        id: {{ order.admin_graphql_api_id | json }}
        customAttributes: {{ attributes | graphql_arguments }}
      }
    ) {
      order {
        id
        name
        customAttributes {
          key
          value
        }
      }
      userErrors {
        field
        message
      }
    }
  }
{% endaction %}
Mechanic Liquid reference

Default option values

Customer metafields and order attributes
{}