Auto-copy customer metafields to new orders, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

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.

Runs Occurs whenever an order is created. Configuration includes customer metafields and order attributes.

15-day free trial – unlimited tasks

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"

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, 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.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
shopify/orders/create
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
customer metafields and order attributes (keyval, required)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more