Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

Occurs whenever an order is created, with a 15 minute delay

What you choose

  • Minutes to wait before removing order attributes
  • Order attributes to remove
  • Remove all order attributes
  • Only run for paid orders

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.

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

{% 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 %}
How task subscriptions work

Option types and flags

Task 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 %}
Mechanic Liquid reference

Default option values

Minutes to wait before removing order attributes
15
Remove all order attributes
false
Only run for paid orders
false