forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
110 lines (96 loc) · 3.85 KB
/
script.liquid
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
{% comment %}
Run the task manually to get the list of your publications and their IDs
{% endcomment %}
{% if event.topic == "mechanic/user/trigger" or event.preview %}
{% capture query %}
query {
publications (first: 30 ) {
nodes {
name
id
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% action "echo" result %}
{% endif %}
{% if event.topic == "shopify/inventory_levels/update" or event.preview %}
{% capture query %}
query {
inventoryLevel(id: {{ inventory_level.admin_graphql_api_id | json }}) {
item {
variant {
displayName
inventoryQuantity
legacyResourceId
product {
legacyResourceId
{% if options.must_belong_to_collection_id -%}
inCollection(id: "gid://shopify/Collection/{{options.must_belong_to_collection_id}}")
{%- endif %}
{% if options.must_be_published_to_publication_id -%}
publishedOnPublication(publicationId: "gid://shopify/Publication/{{options.must_be_published_to_publication_id}}")
{%- endif %}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"inventoryLevel": {
"item": {
"variant": {
"displayName": "Short sleeve t-shirt - L / Red",
"inventoryQuantity": 0,
"legacyResourceId": "1234567890",
"product": {
"legacyResourceId": "1234567890"
{% if options.must_belong_to_collection_id -%}
, "inCollection" : "true"
{%- endif %}
{% if options.must_be_published_to_publication_id -%}
, "publishedOnPublication": "true"
{%- endif %}
}
}
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% action "echo" result %}
{% endif %}
{% assign product_qualifies = false %}
{% assign variantNode = result.data.inventoryLevel.item.variant %}
{% if options.must_belong_to_collection_id != blank and options.must_be_published_to_publication_id != blank %}
{% error "You can only filter by collection or publication, not both." %}
{% endif %}
{% if options.must_belong_to_collection_id == blank and options.must_be_published_to_publication_id == bank %}
{% assign product_qualifies = true %}
{% elsif options.must_belong_to_collection_id and variantNode.product.inCollection %}
{% assign product_qualifies = true %}
{% elsif options.must_be_published_to_publication_id and variantNode.product.publishedOnPublication %}
{% assign product_qualifies = true %}
{% endif %}
{% if variantNode.inventoryQuantity == 0 and product_qualifies %}
{% assign variant_admin_url = "https://" | append: shop.domain | append: "/admin/products/" | append: variantNode.product.legacyResourceId | append: "/variants/" | append: variantNode.legacyResourceId %}
{% assign subject = options.email_subject__required | replace: "VARIANT_TITLE", variantNode.displayName | replace: "VARIANT_ADMIN_URL", variant_admin_url %}
{% assign body = options.email_body__multiline_required | replace: "VARIANT_TITLE", variantNode.displayName | replace: "VARIANT_ADMIN_URL", variant_admin_url %}
{% action "email" %}
{
"to": {{ options.email_recipient__email_required | json }},
"subject": {{ subject | json }},
"body": {{ body | strip | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% endif %}
{% endif %}