Mechanic is a development and ecommerce automation platform for Shopify. :)
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.
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"
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?)
shopify/orders/create
{% 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 %}