Auto-add default custom attributes to new orders, with Mechanic.

Mechanic is a development platform for Shopify. :)

Auto-add default custom attributes to new orders

by Isaac Bowen (team@usemechanic.com)

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.

Runs when an order is created. Configuration includes default order attributes to add.

15-day free trial – unlimited tasks

Documentation

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.

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)
Options
default order attributes to add (keyval, required)
Script
{% 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 tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Default order attributes to add
{"Assigned CSM"=>""}