-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathset-product-or-variant-metafields-in-bulk.json
28 lines (28 loc) · 6.64 KB
/
set-product-or-variant-metafields-in-bulk.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
26
27
28
{
"docs": "Use this task to set a **default value** on a single metafield across all of your products or variants, excluding any where this metafield already has a value set.\n\nConfigure it with a metafield namespace and key (separated by a period, e.g. \"my_fields.color\"), the metafield type, and the default metafield value. Then choose either to *Set product metafields* or *Set variant metafields*.\n\nThe following Shopify metafield types are supported by this task: *boolean*, *color*, *date*, *date_time*, *number_decimal*, *number_integer*, *single_line_text_field*, *url*. More information on Shopify metafield types and the values supported by each can be found [here](https://shopify.dev/apps/metafields/types#supported-types).\n\n**Note:** This task will not validate the configured default metafield value against the configured metafield type when the task is saved. Instead, if an invalid metafield value is configured, then a Shopify GraphQL error will appear in the task run log (specifically the *mechanic/shopify/bulk_operation*\n child event), which will indicate the specific issue with the value.",
"halt_action_run_sequence_on_error": true,
"name": "Set product or variant metafields values in bulk",
"online_store_javascript": null,
"options": {
"metafield_namespace_and_key__required": null,
"metafield_type__required": null,
"metafield_default_value__required": null,
"set_product_metafields__boolean": false,
"set_variant_metafields__boolean": false
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": true,
"preview_event_definitions": [],
"script": "{% assign metafield_namespace_and_key = options.metafield_namespace_and_key__required | split: \".\" %}\n{% assign metafield_namespace = metafield_namespace_and_key[0] %}\n{% assign metafield_key = metafield_namespace_and_key[1] %}\n{% assign metafield_type = options.metafield_type__required %}\n{% assign metafield_default_value = options.metafield_default_value__required %}\n{% assign set_product_metafields = options.set_product_metafields__boolean %}\n{% assign set_variant_metafields = options.set_variant_metafields__boolean %}\n\n{% if set_product_metafields and set_variant_metafields %}\n {% error \"You can only choose one option to target, either product metafields or variant metafields, not both at the same time.\" %}\n{% endif %}\n\n{% unless set_product_metafields or set_variant_metafields %}\n {% error \"You must choose one option to target, either product metafields or variant metafields.\" %}\n{% endunless %}\n\n{% if set_product_metafields %}\n {% assign object_type = \"Product\" %}\n{% elsif set_variant_metafields %}\n {% assign object_type = \"ProductVariant\" %}\n{% endif %}\n\n{% if metafield_namespace == blank or metafield_key == blank %}\n {% error \"Metafield namespace and key should be entered together, separated only by a period (e.g. 'my_fields.color').\" %}\n{% endif %}\n\n{% assign allowed_metafield_types = \"boolean,color,date,date_time,number_decimal,number_integer,single_line_text_field,url\" | split: \",\" %}\n\n{% unless allowed_metafield_types contains metafield_type %}\n {% error %}\n {{ allowed_metafield_types | join: \", \" | prepend: \"Metafield type must be one of: \" | json }}\n {% enderror %}\n{% endunless %}\n\n{% if event.topic == \"mechanic/user/trigger\" %}\n {% capture bulk_operation_query %}\n query {\n {% if set_product_metafields %}\n products {\n {% elsif set_variant_metafields %}\n productVariants {\n {% endif %}\n edges {\n node {\n __typename\n id\n metafield(\n namespace: {{ metafield_namespace | json }}\n key: {{ metafield_key | json }}\n ) {\n __typename\n id\n namespace\n key\n type\n value\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% action \"shopify\" %}\n mutation {\n bulkOperationRunQuery(\n query: {{ bulk_operation_query | json }}\n ) {\n bulkOperation {\n id\n status\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n\n{% elsif event.topic == \"mechanic/shopify/bulk_operation\" %}\n {% assign objects = bulkOperation.objects | where: \"__typename\", object_type %}\n\n {% if event.preview %}\n {% assign objects = array %}\n {% assign objects[0] = hash %}\n {% if set_product_metafields %}\n {% assign objects[0][\"id\"] = \"gid://shopify/Product/1234567890\" %}\n {% elsif set_variant_metafields %}\n {% assign objects[0][\"id\"] = \"gid://shopify/ProductVariant/1234567890\" %}\n {% endif %} \n {% endif %}\n\n {% assign metafields_to_set = array %}\n\n {% for object in objects %}\n {% if object.metafield != blank %}\n {% log\n message: \"This object already has a value set for the configured metafield; skipping.\",\n object: object\n %}\n {% continue %}\n {% endif %}\n\n {% assign metafield_to_set = hash %}\n {% assign metafield_to_set[\"ownerId\"] = object.id %}\n {% assign metafield_to_set[\"namespace\"] = metafield_namespace %}\n {% assign metafield_to_set[\"key\"] = metafield_key %}\n {% assign metafield_to_set[\"type\"] = metafield_type %}\n {% assign metafield_to_set[\"value\"] = metafield_default_value %}\n\n {% assign metafields_to_set = metafields_to_set | push: metafield_to_set %}\n {% endfor %}\n\n {% log\n object_type: object_type,\n objects_count: objects.size,\n metafields_to_set_count: metafields_to_set.size\n %}\n\n {% assign groups_of_metafields_to_set = metafields_to_set | in_groups_of: 25, fill_with: false %}\n\n {% for group_of_metafields_to_set in groups_of_metafields_to_set %}\n {% action \"shopify\" %}\n mutation {\n metafieldsSet(\n metafields: {{ group_of_metafields_to_set | graphql_arguments }}\n ) {\n metafields {\n id\n namespace\n key\n type\n value\n owner {\n ... on {{ object_type }} {\n id\n }\n }\n }\n userErrors {\n code\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n{% endif %}",
"subscriptions": [
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation"
],
"subscriptions_template": "mechanic/user/trigger\nmechanic/shopify/bulk_operation",
"tags": [
"Bulk",
"Metafields",
"Products",
"Variants"
]
}