Auto-remove attributes on new orders after X minutes, with Mechanic.

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

Auto-remove attributes on new orders after X minutes

This task will remove order attributes from newly created or paid orders after a configurable amount of minutes. This can be helpful when order attributes are set by apps or other processes that are not necessary to keep on the order after initial processing.

Runs Occurs whenever an order is created, with a 15 minute delay. Configuration includes minutes to wait before removing order attributes, order attributes to remove, remove all order attributes, and only run for paid orders.

15-day free trial – unlimited tasks

Documentation

This task will remove order attributes from newly created or paid orders after a configurable amount of minutes. This can be helpful when order attributes are set by apps or other processes that are not necessary to keep on the order after initial processing.

Either configure one or more exact order attributes to be removed (e.g. "_app_track_id"), or choose the _Remove all order attributes option.

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
{% assign delay = options.minutes_to_wait_before_removing_order_attributes__number_required | default: 0 %}
{% if options.only_run_for_paid_orders__boolean %}
  shopify/orders/paid+{{ delay }}.minutes
{% else %}  
  shopify/orders/create+{{ delay }}.minutes
{% endif %}
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
minutes to wait before removing order attributes (number, required), order attributes to remove (array), remove all order attributes (boolean), only run for paid orders (boolean)
Code
{% comment %}
  -- sort this option to the top
  {{ options.minutes_to_wait_before_removing_order_attributes__number_required }}
{% endcomment %}

{% assign attributes_to_remove = options.order_attributes_to_remove__array %}
{% assign remove_all_attributes = options.remove_all_order_attributes__boolean %}

{% unless remove_all_attributes or attributes_to_remove != blank %}
  {% error "Please choose either the 'Remove all order attributes' option, or enter one or more order attributes to remove" %}
  {% break %}
{% endunless %}

{% capture query %}
  query {
    order(id: {{ order.admin_graphql_api_id | json }}) {
      id
      customAttributes {
        key
        value
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "order": {
          "id": "gid://shopify/Order/1234567890",
          "customAttributes": [
            {
              "key": "preview",
              "value": "keep_this_one"
            },
            {
              "key": {{ attributes_to_remove.first | json }},
              "value": "remove_this_one"
            }
          ]
        }
      }
    }
  {% endcapture %}

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

{% assign order_attributes = result.data.order.customAttributes %}

{% assign attributes_to_keep = array %}

{% unless remove_all_attributes %}
  {% for attribute in order_attributes %}
    {% unless attributes_to_remove contains attribute.key %}
      {% assign attributes_to_keep = attributes_to_keep | push: attribute %}
    {% endunless %}
  {% endfor %}
{% endunless %}

{% if attributes_to_keep == order_attributes %}
  {% log
    message: "No order attributes exist, or none matched the configured order attributes to remove",
    attributes_to_remove: attributes_to_remove,
    order_attributes: order_attributes
  %}
  {% break %}
{% endif %}

{% action "shopify" %}
  mutation {
    orderUpdate(
      input: {
        id: {{ order.admin_graphql_api_id | json }}
        customAttributes: {{ attributes_to_keep | 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
Defaults
Minutes to wait before removing order attributes
15