forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrack-incoming-donations-in-a-store-metafield.json
25 lines (25 loc) · 5.33 KB
/
track-incoming-donations-in-a-store-metafield.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
25
{
"docs": "This task watches for orders for a certain donation product, and tallies up the total donation amount in a store metafield, allowing you to display this value in your online storefront.\n\nThis task runs automatically, whenever an order is paid. To fully recalculate the total donation amount stored, click the \"Run task\" button. Optionally, choose to have this task run the recalculation nightly - useful for making sure that refunds are regularly accounted for.",
"halt_action_run_sequence_on_error": false,
"name": "Track incoming donations in a store metafield",
"online_store_javascript": null,
"options": {
"metafield_namespace__required": null,
"metafield_key__required": null,
"donation_product_handle__required": null,
"recalculate_nightly__boolean": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign metafield_namespace = options.metafield_namespace__required %}\n{% assign metafield_key = options.metafield_key__required %}\n{% assign donation_product_handle = options.donation_product_handle__required %}\n\n{% capture query %}\n query {\n shop {\n id\n metafield(\n namespace: {{ metafield_namespace | json }}\n key: {{ metafield_key | json }}\n ) {\n value\n }\n }\n }\n{% endcapture %}\n\n{% assign result = query | shopify %}\n\n{% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"shop\": {\n \"id\": \"gid://shopify/Shop/1234567890\",\n \"metafield\": {\n \"value\": \"123.45\"\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n{% endif %}\n\n{% assign shop = result.data.shop %}\n\n{% assign metafield_donation_total = shop.metafield.value | times: 1 %}\n{% assign donation_total = 0 %}\n\n{% if event.topic == \"mechanic/user/trigger\" or event.topic contains \"mechanic/scheduler/\" %}\n {% assign cursor = nil %}\n\n {% for n in (0..10000) %}\n {% capture query %}\n query {\n orders(\n first: 10\n after: {{ cursor | json }}\n query: \"financial_status:paid\"\n ) {\n pageInfo {\n hasNextPage\n }\n edges {\n cursor\n node {\n id\n lineItems(first: 25) {\n edges {\n node {\n id\n product {\n handle\n }\n discountedUnitPriceSet {\n shopMoney {\n amount\n }\n }\n }\n }\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% assign orders = result.data.orders.edges | map: \"node\" %}\n\n {% for order in orders %}\n {% assign line_items = order.lineItems.edges | map: \"node\" %}\n\n {% for line_item in line_items %}\n {% if line_item.product.handle != donation_product_handle %}\n {% continue %}\n {% endif %}\n\n {% assign donation_total = donation_total | plus: line_item.discountedUnitPriceSet.shopMoney.amount %}\n {% endfor %}\n {% endfor %}\n\n {% if result.data.orders.pageInfo.hasNextPage %}\n {% assign cursor = result.data.orders.edges.last.cursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n{% elsif event.topic == \"shopify/orders/create\" or event.topic == \"shopify/orders/paid\" %}\n {% assign donation_total = metafield_donation_total %}\n\n {% if order.financial_status == \"paid\" %}\n {% assign current_donation = 0 %}\n\n {% for line_item in order.line_items %}\n {% if line_item.product.handle != donation_product_handle %}\n {% continue %}\n {% endif %}\n\n {% assign current_donation = current_donation | plus: line_item.price | minus: line_item.total_discount %}\n {% endfor %}\n\n {% if current_donation > 0 %}\n {% assign donation_total = donation_total | plus: current_donation %}\n {% endif %}\n {% endif %}\n{% endif %}\n\n{% if donation_total != metafield_donation_total or event.preview %}\n {% action \"shopify\" %}\n mutation {\n metafieldsSet(\n metafields: [\n {\n ownerId: {{ shop.id | json }}\n namespace: {{ metafield_namespace | json }}\n key: {{ metafield_key | json }}\n value: {{ donation_total | append: \"\" | json }}\n type: \"number_decimal\"\n }\n ]\n ) {\n metafields {\n id\n namespace\n key\n type\n value\n owner {\n ... on Shop {\n id\n name\n myshopifyDomain\n }\n }\n }\n userErrors {\n code\n field\n message\n }\n }\n }\n {% endaction %}\n{% endif %}",
"subscriptions": [
"mechanic/user/trigger",
"shopify/orders/create",
"shopify/orders/paid"
],
"subscriptions_template": "mechanic/user/trigger\r\n{% if options.recalculate_nightly__boolean %}\r\n mechanic/scheduler/daily\r\n{% endif %}\r\nshopify/orders/create\r\nshopify/orders/paid",
"tags": [
"Metafields",
"Products"
]
}