forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaintain-collections-by-product-metafield-values.json
26 lines (26 loc) · 8.53 KB
/
maintain-collections-by-product-metafield-values.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
{
"docs": "This task will scan your entire catalog to maintain products in collections based on metafield values. Configure the task with a metafield namespace and key (separated by a period, e.g. \"custom.color\"), and a list of metafield string values paired with collection IDs. If the value of a product's metafield matches any of the configured ones, then the product will be added to that collection. Conversely, products in a configured collection which do not match a metafield value will be removed.\n\n**Important notes:**\n- This task only checks the values of metafields that are of type *single_line_text_field* or *list.single_line_text_field*. For list type metafields, the product will qualify for collection membership if *any* of the list values matches a metafield value. More information on Shopify metafield types can be found [here](https://shopify.dev/apps/metafields/types#supported-types).\n- Adding and removing products from collections are both handled in the background by Shopify jobs, after the Mechanic task run is complete. Expect a delay in final results in your shop for task runs involving very large additions or removals.",
"halt_action_run_sequence_on_error": false,
"name": "Maintain collections by product metafield values",
"online_store_javascript": null,
"options": {
"metafield_namespace_and_key__required": null,
"metafield_values_and_collection_ids__keyval_number_required": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"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_values_and_collection_ids = options.metafield_values_and_collection_ids__keyval_number_required %}\n{% assign metafield_values = metafield_values_and_collection_ids | keys %}\n{% assign collection_ids = metafield_values_and_collection_ids | values %}\n\n{% if event.topic == \"mechanic/user/trigger\" or event.topic contains \"mechanic/scheduler/\" %}\n {% assign search_query = nil %}\n\n {% capture bulk_operation_query %}\n query {\n products {\n edges {\n node {\n __typename\n id\n {% for collection_id in collection_ids %}\n in_collection_{{ collection_id }}: inCollection(id: \"gid://shopify/Collection/{{ collection_id }}\")\n {% endfor %}\n metafields(namespace: {{ metafield_namespace | json }}) {\n edges {\n node {\n __typename\n id\n key\n type\n value\n }\n }\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 {% if event.preview %}\n {% capture jsonl_string %}\n {\"__typename\":\"Product\",\"id\":\"gid:\\/\\/shopify\\/Product\\/1234567890\",\"in_collection_{{ collection_ids[0] }}\":false}\n {\"__typename\":\"Metafield\",\"key\":{{ metafield_key | json }},\"type\":\"single_line_text_field\",\"value\":{{ metafield_values[0] | json }},\"__parentId\":\"gid:\\/\\/shopify\\/Product\\/1234567890\"}\n {\"__typename\":\"Product\",\"id\":\"gid:\\/\\/shopify\\/Product\\/2345678901\",\"in_collection_{{ collection_ids[0] }}\":false}\n {\"__typename\":\"Metafield\",\"key\":{{ metafield_key | json }},\"type\":\"list.single_line_text_field\",\"value\":{{ metafield_values | json | json }},\"__parentId\":\"gid:\\/\\/shopify\\/Product\\/2345678901\"}\n {% endcapture %}\n\n {% assign bulkOperation = hash %}\n {% assign bulkOperation[\"objects\"] = jsonl_string | parse_jsonl %}\n {% endif %}\n\n {% assign bulk_products = bulkOperation.objects | where: \"__typename\", \"Product\" %}\n {% assign bulk_metafields = bulkOperation.objects | where: \"__typename\", \"Metafield\" %}\n\n {% assign collection_ids_and_product_ids_to_add = hash %}\n {% assign collection_ids_and_product_ids_to_remove = hash %}\n\n {% for product in bulk_products %}\n {% assign product_metafields = bulk_metafields | where: \"__parentId\", product.id %}\n\n {% for keyval in metafield_values_and_collection_ids %}\n {% assign metafield_value_to_match = keyval[0] %}\n {% assign collection_id = keyval[1] %}\n {% assign collection_gid = \"gid://shopify/Collection/\" | append: collection_id %}\n {% assign in_collection_alias = \"in_collection_\" | append: collection_id %}\n {% assign product_in_collection = product[in_collection_alias] %}\n\n {% assign product_should_be_in_collection = false %}\n\n {% for product_metafield in product_metafields %}\n {% if product_metafield.type == \"list.single_line_text_field\" %}\n {% assign product_metafield_values = product_metafield.value | parse_json %}\n {% elsif product_metafield.type == \"single_line_text_field\" %}\n {% assign product_metafield_values = array | push: product_metafield.value %}\n {% endif %}\n\n {% for product_metafield_value in product_metafield_values %}\n {% if product_metafield_value == metafield_value_to_match %}\n {% assign product_should_be_in_collection = true %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% if product_should_be_in_collection %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% if product_should_be_in_collection %}\n {% unless product_in_collection %}\n {% assign collection_ids_and_product_ids_to_add[collection_gid]\n = collection_ids_and_product_ids_to_add[collection_gid]\n | default: array\n | push: product.id\n %}\n {% endunless %}\n\n {% elsif product_in_collection %}\n {% assign collection_ids_and_product_ids_to_remove[collection_gid]\n = collection_ids_and_product_ids_to_remove[collection_gid]\n | default: array\n | push: product.id\n %}\n {% endif %}\n {% endfor %}\n {% endfor %}\n\n {% for collection_id in collection_ids %}\n {% assign collection_gid = \"gid://shopify/Collection/\" | append: collection_id %}\n {% assign product_ids_to_add = collection_ids_and_product_ids_to_add[collection_gid] | uniq %}\n {% assign product_ids_to_remove = collection_ids_and_product_ids_to_remove[collection_gid] | uniq %}\n\n {% if product_ids_to_add != blank %}\n {% assign groups_of_product_ids_to_add = product_ids_to_add | in_groups_of: 250, fill_with: false %}\n\n {% for group_of_product_ids_to_add in groups_of_product_ids_to_add %}\n {% action \"shopify\" %}\n mutation {\n collectionAddProductsV2(\n id: {{ collection_gid | json }}\n productIds: {{ group_of_product_ids_to_add | json }}\n ) {\n job {\n id\n done\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n {% endif %}\n\n {% if product_ids_to_remove != blank %}\n {% assign groups_of_product_ids_to_remove = product_ids_to_remove | in_groups_of: 250, fill_with: false %}\n\n {% for group_of_product_ids_to_remove in groups_of_product_ids_to_remove %}\n {% action \"shopify\" %}\n mutation {\n collectionRemoveProducts(\n id: {{ collection_gid | json }}\n productIds: {{ group_of_product_ids_to_remove | json }}\n ) {\n job {\n id\n done\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n {% endif %}\n {% endfor %}\n{% endif %}\n",
"subscriptions": [
"mechanic/shopify/bulk_operation",
"mechanic/scheduler/daily",
"mechanic/user/trigger"
],
"subscriptions_template": "mechanic/shopify/bulk_operation\nmechanic/scheduler/daily \nmechanic/user/trigger",
"tags": [
"Bulk",
"Collections",
"Metafields",
"Products"
]
}