Mechanic is a development and ecommerce automation platform for Shopify. :)
This task applies the tag of your choice to orders, based on the highest risk level for each order. Run this task manually to tag all open orders in your shop, according to their respective risk levels.
Runs Occurs whenever an order is updated, Occurs when a user manually triggers the task, and Occurs when a bulk operation is completed. Configuration includes risk levels and tags.
This task applies the tag of your choice to orders, based on the highest risk level for each order. Run this task manually to tag all open orders in your shop, according to their respective risk levels.
Valid risk levels: LOW, MEDIUM, HIGH.
Feel free to remove a tag from this task's options, if you don't need it. :)
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/updated mechanic/user/trigger mechanic/shopify/bulk_operation
{% assign risk_levels_and_tags = options.risk_levels_and_tags__keyval_required %} {% assign risk_levels = risk_levels_and_tags | keys %} {% assign risk_tags = risk_levels_and_tags | values %} {% comment %} -- check that only valid risk levels have been configured {% endcomment %} {% assign valid_risk_levels = "HIGH,MEDIUM,LOW" | split: "," %} {% for risk_level in risk_levels %} {% unless valid_risk_levels contains risk_level %} {% error %} {{ risk_level | json | prepend: "Unknown risk level " | append: ". Valid risk level values: HIGH, MEDIUM, LOW" | json }} {% enderror %} {% endunless %} {% endfor %} {% if event.topic == "shopify/orders/updated" %} {% capture query %} query { order(id: {{ order.admin_graphql_api_id | json }}) { id closed tags risk { assessments { riskLevel } } } } {% endcapture %} {% assign result = query | shopify %} {% comment %} -- don't make tagging updates to closed orders {% endcomment %} {% if order.closed %} {% break %} {% endif %} {% assign orders = array | push: result.data.order %} {% elsif event.topic == "mechanic/user/trigger" %} {% comment %} -- get all open orders {% endcomment %} {% capture bulk_operation_query %} query { orders( query: "status:open" ) { edges { node { __typename id tags risk { assessments { riskLevel } } } } } } {% endcapture %} {% action "shopify" %} mutation { bulkOperationRunQuery( query: {{ bulk_operation_query | json }} ) { bulkOperation { id status } userErrors { field message } } } {% endaction %} {% elsif event.topic == "mechanic/shopify/bulk_operation" %} {% assign orders = bulkOperation.objects | where: "__typename", "Order" %} {% endif %} {% if event.preview %} {% capture orders_json %} [ { "id": "gid://shopify/Order/1234567890", "risk": { "assessments": [ { "riskLevel": {{ valid_risk_levels.first | json }} } ] } } ] {% endcapture %} {% assign orders = orders_json | parse_json %} {% endif %} {% for order in orders %} {% assign tag_should_have = nil %} {% assign tag_to_add = nil %} {% assign tags_to_remove = array %} {% comment %} -- get the risk level from each risk assessment {% endcomment %} {% assign order_risk_levels = order.risk.assessments | map: "riskLevel" %} {% comment %} -- check risk levels, highest to lowest, so the order only gets tagged with the most severe level configured {% endcomment %} {% for valid_risk_level in valid_risk_levels %} {% if order_risk_levels contains valid_risk_level %} {% if risk_levels contains valid_risk_level %} {% assign tag_should_have = risk_levels_and_tags[valid_risk_level] %} {% break %} {% endif %} {% endif %} {% endfor %} {% unless order.tags contains tag_should_have %} {% assign tag_to_add = tag_should_have %} {% endunless %} {% comment %} -- check if the order has any tags to remove {% endcomment %} {% for risk_tag in risk_tags %} {% unless risk_tag == tag_should_have %} {% if order.tags contains risk_tag %} {% assign tags_to_remove = tags_to_remove | push: risk_tag %} {% endif %} {% endunless %} {% endfor %} {% comment %} -- update tags as needed {% endcomment %} {% if tag_to_add != blank or tags_to_remove != blank %} {% action "shopify" %} mutation { {% if tag_to_add != blank %} tagsAdd( id: {{ order.id | json }} tags: {{ tag_to_add | json }} ) { userErrors { field message } } {% endif %} {% if tags_to_remove != blank %} tagsRemove( id: {{ order.id | json }} tags: {{ tags_to_remove | json }} ) { userErrors { field message } } {% endif %} } {% endaction %} {% endif %} {% endfor %}
{"Low" => "low-risk", "Medium" => "medium-risk", "High" => "high-risk"}