forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-add-the-draft-order-id-to-an-orders-attributes.json
21 lines (21 loc) · 4.15 KB
/
auto-add-the-draft-order-id-to-an-orders-attributes.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"docs": "When a new order is created from a draft order, this task will automatically add the draft order ID and/or name to the custom attributes of the new order.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-add the draft order to a new order's attributes",
"online_store_javascript": null,
"options": {
"order_attribute_name_for_storing_the_draft_order_id": "Draft Order ID",
"order_attribute_name_for_storing_the_draft_order_name": "Draft Order Name"
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% if options.order_attribute_name_for_storing_the_draft_order_id == blank and options.order_attribute_name_for_storing_the_draft_order_name == blank %}\n {% error \"Please fill in at least one order attribute name.\" %}\n{% endif %}\n\n{% if event.preview %}\n {% assign order = hash %}\n {% assign order[\"admin_graphql_api_id\"] = \"gid://shopify/Order/1234567890\" %}\n {% assign order[\"note_attributes\"] = array %}\n {% assign order[\"note_attributes\"][0] = hash %}\n {% assign order[\"note_attributes\"][0][\"name\"] = \"Existing attribute name\" %}\n {% assign order[\"note_attributes\"][0][\"value\"] = \"Existing attribute value\" %}\n{% endif %}\n\n{% capture query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n events(first: 1, sortKey: CREATED_AT) {\n edges {\n node {\n message\n }\n }\n }\n }\n }\n{% endcapture %}\n\n{% assign result = query | shopify %}\n\n{% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"order\": {\n \"events\": {\n \"edges\": [\n {\n \"node\": {\n \"message\": \"Staff Member created this order from draft order <a href=\\\"https://example.myshopify.com/admin/draft_orders/1234567890\\\">#1000</a>.\"\n }\n }\n ]\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n{% endif %}\n\n{% assign event_message = result.data.order.events.edges.first.node.message %}\n{% if event_message contains \".myshopify.com/admin/draft_orders/\" %}\n {% assign draft_order_id = event_message | split: \"/draft_orders/\" | last | split: '\"' | first %}\n {% assign draft_order_name = event_message | split: \">#\" | last | split: \"</a>\" | first | prepend: \"#\" %}\n\n {% if draft_order_id == blank %}\n {% log message: \"Couldn't locate the draft order ID\", event_message: event_message %}\n {% else %}\n {% action \"shopify\" %}\n mutation {\n orderUpdate(\n input: {\n id: {{ order.admin_graphql_api_id | json }}\n customAttributes: [\n {% for note_attribute in order.note_attributes %}\n {\n key: {{ note_attribute.name | json }}\n value: {{ note_attribute.value | json }}\n }\n {% endfor %}\n {% if options.order_attribute_name_for_storing_the_draft_order_id != blank %}\n {\n key: {{ options.order_attribute_name_for_storing_the_draft_order_id | json }}\n value: {{ draft_order_id | json }}\n }\n {% endif %}\n {% if options.order_attribute_name_for_storing_the_draft_order_name != blank %}\n {\n key: {{ options.order_attribute_name_for_storing_the_draft_order_name | json }}\n value: {{ draft_order_name | json }}\n }\n {% endif %}\n ]\n }\n ) {\n order {\n customAttributes {\n key\n value\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n{% else %}\n {% log message: \"Event message didn't mention a draft order\", event_message: event_message %}\n{% endif %}",
"subscriptions": [
"shopify/orders/create"
],
"subscriptions_template": "shopify/orders/create",
"tags": [
"Draft Orders",
"Order Attributes"
]
}