forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-pay-orders-from-customers-with-a-certain-tag.json
24 lines (24 loc) · 5.08 KB
/
auto-pay-orders-from-customers-with-a-certain-tag.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"docs": "Use this task to immediately bump orders, from certain customers, to \"paid\". Useful if you have a standing payment arrangement for certain customers. Optionally, auto-remove this tag from customers after auto-paying their order. Useful for using customer tags as one-time triggers for auto-payment.\n\nThis task runs automatically for incoming orders. Run this task manually to scan your store's order history, processing orders that were previously placed. (To scan orders older than 60 days, [enable \"Read all orders\"](https://help.usemechanic.com/tutorials/enabling-read_all_orders).)\r\n\r\nEnable the \"Remove tag from customer after processing their order\" option to have this task remove the required tag whenever this task marks an order of theirs as paid. (If run manually, this task will still mark all of the qualifying customer's orders as paid, not just one order.)",
"halt_action_run_sequence_on_error": false,
"name": "Auto-pay orders from customers with a certain tag",
"online_store_javascript": null,
"options": {
"required_customer_tag__required": "autopay",
"remove_tag_from_customer_after_processing_their_order__boolean": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign tag_to_match = options.required_customer_tag__required | downcase %}\n\n{% if event.topic contains \"shopify/orders\" %}\n {% assign customer_tags = order.customer.tags | downcase | split: \", \" %}\n\n {% capture order_query %}\n query {\n order(id: {{ order.admin_graphql_api_id | json }}) {\n canMarkAsPaid\n }\n }\n {% endcapture %}\n\n {% assign order_result = order_query | shopify %}\n\n {% if event.preview or customer_tags contains tag_to_match %}\n {% if order_result.data.order.canMarkAsPaid == false %}\n {\"log\": \"Order cannot be manually marked as paid - skipping.\"}\n {% else %}\n {% action \"shopify\" %}\n mutation {\n orderMarkAsPaid(input: {\n id: {{ order.admin_graphql_api_id | json }}\n }) {\n order {\n fullyPaid\n }\n userErrors {\n field\n message\n }\n }\n\n {% if options.remove_tag_from_customer_after_processing_their_order__boolean %}\n tagsRemove(\n id: {% if event.preview %}\"gid://shopify/Customer/1234567890\"{% else %}{{ order.customer.admin_graphql_api_id | json }}{% endif %}\n tags: {{ options.required_customer_tag__required | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n {% endif %}\n }\n {% endaction %}\n {% endif %}\n {% endif %}\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% assign cursor = nil %}\n\n {% for n in (0..100) %}\n {% capture orders_query %}\n query {\n orders(\n first: 250\n after: {{ cursor | json }}\n query: \"-financial_status:paid\"\n ) {\n pageInfo {\n hasNextPage\n }\n edges {\n cursor\n node {\n id\n canMarkAsPaid\n customer {\n id\n tags\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign orders_result = orders_query | shopify %}\n\n {% for edge in orders_result.data.orders.edges %}\n {% assign order_node = edge.node %}\n\n {% if order_node.canMarkAsPaid %}\n {% assign customer_tags = order_node.customer.tags | join: \", \" | downcase | split: \", \" %}\n\n {% if customer_tags contains tag_to_match %}\n {% action \"shopify\" %}\n mutation {\n orderMarkAsPaid(input: {\n id: {{ order_node.id | json }}\n }) {\n order {\n fullyPaid\n }\n userErrors {\n field\n message\n }\n }\n\n {% if options.remove_tag_from_customer_after_processing_their_order__boolean %}\n tagsRemove(\n id: {% if event.preview %}\"gid://shopify/Customer/1234567890\"{% else %}{{ order_node.customer.id | json }}{% endif %}\n tags: {{ options.required_customer_tag__required | json }}\n ) {\n userErrors {\n field\n message\n }\n }\n {% endif %}\n }\n {% endaction %}\n {% endif %}\n {% endif %}\n {% endfor %}\n\n {% if orders_result.data.orders.pageInfo.hasNextPage %}\n {% assign cursor = orders_result.data.orders.edges.last.cursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n{% endif %}",
"subscriptions": [
"shopify/orders/create",
"mechanic/user/trigger"
],
"subscriptions_template": "shopify/orders/create\nmechanic/user/trigger",
"tags": [
"Orders",
"Payment",
"Tag",
"Watch"
]
}