Mechanic Get Mechanic
Task library

Auto-add default custom attributes to new orders

Shopify allows orders to have custom attributes associated with them – and these attributes can be edited from within Shopify. Use this task to configure default attributes to be added to all new orders, providing an attribute name and (optionally) a value. Feel free to leave the value blank if you know you'll want to add your own values later, within Shopify.

Try this task

When it runs

Occurs whenever an order is created

What you choose

Default order attributes to add

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

{% if event.preview %}
  {% assign order = hash %}
  {% assign order["admin_graphql_api_id"] = "gid://shopify/Order/1234567890" %}
  {% assign order["note_attributes"] = array %}
  {% assign order["note_attributes"][0] = hash %}
  {% assign order["note_attributes"][0]["name"] = "Existing attribute name" %}
  {% assign order["note_attributes"][0]["value"] = "Existing attribute value" %}
{% endif %}

{% assign attributes = array %}

{% for pair in options.default_order_attributes_to_add__keyval_required %}
  {% assign attribute = hash %}
  {% assign attribute["key"] = pair[0] %}
  {% assign attribute["value"] = "" | append: pair[1] %}
  {% assign attributes[attributes.size] = attribute %}
{% endfor %}

{% for note_attribute in order.note_attributes %}
  {% assign attribute = hash %}
  {% assign attribute["key"] = note_attribute.name %}
  {% assign attribute["value"] = note_attribute.value %}
  {% assign attributes[attributes.size] = attribute %}
{% endfor %}

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

Default option values

Default order attributes to add
{"Assigned CSM" => ""}