-
Notifications
You must be signed in to change notification settings - Fork 33
/
script.liquid
150 lines (133 loc) · 4.59 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
{% assign metafield_namespace_and_key = options.metafield_namespace_and_key__required | split: "." %}
{% assign metafield_namespace = metafield_namespace_and_key[0] %}
{% assign metafield_key = metafield_namespace_and_key[1] %}
{% assign metafield_type = options.metafield_type__required %}
{% assign metafield_default_value = options.metafield_default_value__required %}
{% assign set_product_metafields = options.set_product_metafields__boolean %}
{% assign set_variant_metafields = options.set_variant_metafields__boolean %}
{% if set_product_metafields and set_variant_metafields %}
{% error "You can only choose one option to target, either product metafields or variant metafields, not both at the same time." %}
{% endif %}
{% unless set_product_metafields or set_variant_metafields %}
{% error "You must choose one option to target, either product metafields or variant metafields." %}
{% endunless %}
{% if set_product_metafields %}
{% assign object_type = "Product" %}
{% elsif set_variant_metafields %}
{% assign object_type = "ProductVariant" %}
{% endif %}
{% if metafield_namespace == blank or metafield_key == blank %}
{% error "Metafield namespace and key should be entered together, separated only by a period (e.g. 'my_fields.color')." %}
{% endif %}
{% assign allowed_metafield_types = "boolean,color,date,date_time,number_decimal,number_integer,single_line_text_field,url" | split: "," %}
{% unless allowed_metafield_types contains metafield_type %}
{% error %}
{{ allowed_metafield_types | join: ", " | prepend: "Metafield type must be one of: " | json }}
{% enderror %}
{% endunless %}
{% if event.topic == "mechanic/user/trigger" %}
{% capture bulk_operation_query %}
query {
{% if set_product_metafields %}
products {
{% elsif set_variant_metafields %}
productVariants {
{% endif %}
edges {
node {
__typename
id
metafield(
namespace: {{ metafield_namespace | json }}
key: {{ metafield_key | json }}
) {
__typename
id
namespace
key
type
value
}
}
}
}
}
{% 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 objects = bulkOperation.objects | where: "__typename", object_type %}
{% if event.preview %}
{% assign objects = array %}
{% assign objects[0] = hash %}
{% if set_product_metafields %}
{% assign objects[0]["id"] = "gid://shopify/Product/1234567890" %}
{% elsif set_variant_metafields %}
{% assign objects[0]["id"] = "gid://shopify/ProductVariant/1234567890" %}
{% endif %}
{% endif %}
{% assign metafields_to_set = array %}
{% for object in objects %}
{% if object.metafield != blank %}
{% log
message: "This object already has a value set for the configured metafield; skipping.",
object: object
%}
{% continue %}
{% endif %}
{% assign metafield_to_set = hash %}
{% assign metafield_to_set["ownerId"] = object.id %}
{% assign metafield_to_set["namespace"] = metafield_namespace %}
{% assign metafield_to_set["key"] = metafield_key %}
{% assign metafield_to_set["type"] = metafield_type %}
{% assign metafield_to_set["value"] = metafield_default_value %}
{% assign metafields_to_set = metafields_to_set | push: metafield_to_set %}
{% endfor %}
{% log
object_type: object_type,
objects_count: objects.size,
metafields_to_set_count: metafields_to_set.size
%}
{% assign groups_of_metafields_to_set = metafields_to_set | in_groups_of: 25, fill_with: false %}
{% for group_of_metafields_to_set in groups_of_metafields_to_set %}
{% action "shopify" %}
mutation {
metafieldsSet(
metafields: {{ group_of_metafields_to_set | graphql_arguments }}
) {
metafields {
id
namespace
key
type
value
owner {
... on {{ object_type }} {
id
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% endfor %}
{% endif %}