forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-inventory-levels-to-variant-metafields.json
27 lines (27 loc) · 7.55 KB
/
sync-inventory-levels-to-variant-metafields.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
{
"docs": "Use this task to easily make your inventory levels available in your online storefront theme. This task monitors for changes to inventory, and copies inventory availability figures to metafields on the related variant.\n\nThis task runs automatically, whenever an inventory level is adjusted or whenever a variant is stocked for the first time at a new location.\n\nRun this task manually to backfill metafields for all variants that are stocked in the locations you've configured in this task.\n\n\"Location IDs and metafield keys\" is a [keyval option](https://learn.mechanic.dev/core/tasks/options#flags), where the left-hand side (the _key_) should contain your location IDs, and the right-hand side (the _value_) should contain the \"metafield key\" that you want to associate with each location (e.g. \"1234567890\" <> \"shop\", \"2345678901\" <> \"warehouse\").\n \n[Learn how to find location IDs here.](https://learn.mechanic.dev/techniques/finding-a-resource-id)",
"halt_action_run_sequence_on_error": false,
"name": "Sync inventory levels to variant metafields",
"online_store_javascript": null,
"options": {
"metafield_namespace__required": null,
"location_ids_and_metafield_keys__keyval_required": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign metafield_namespace = options.metafield_namespace__required | strip %}\n{% assign location_ids_and_metafield_keys = options.location_ids_and_metafield_keys__keyval_required %}\n\n{% if event.topic contains \"shopify/inventory_levels/\" %}\n {% if event.preview %}\n {% capture inventory_level_json %}\n {\n \"location_id\": {{ location_ids_and_metafield_keys.first.first }},\n \"admin_graphql_api_id\": \"gid://shopify/InventoryLevel/1234567890?inventory_item_id=1234567890\"\n }\n {% endcapture %}\n\n {% assign inventory_level = inventory_level_json | parse_json %}\n {% endif %}\n\n {% assign inventory_level_string = inventory_level.location_id | append: \"\" %}\n {% assign metafield_key = location_ids_and_metafield_keys[inventory_level_string] %}\n\n {% if metafield_key != blank %}\n {% capture query %}\n query {\n inventoryLevel(id: {{ inventory_level.admin_graphql_api_id | json }}) {\n id\n quantities(names: \"available\") {\n name\n quantity\n }\n item {\n variant {\n id\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"inventoryLevel\": {\n \"quantities\": [\n {\n \"name\": \"available\",\n \"quantity\": 20\n }\n ],\n \"item\": {\n \"variant\": {\n \"id\": \"gid://shopify/ProductVariant/1234567890\"\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% action \"shopify\" %}\n mutation {\n metafieldsSet(\n metafields: [\n {\n ownerId: {{ result.data.inventoryLevel.item.variant.id | json }}\n namespace: {{ metafield_namespace | json }}\n key: {{ metafield_key | json }}\n type: \"number_integer\"\n value: {{ result.data.inventoryLevel.quantities.first.quantity | append: \"\" | json }}\n }\n ]\n ) {\n metafields {\n id\n namespace\n key\n type\n value\n owner {\n ... on ProductVariant {\n id\n }\n }\n }\n userErrors {\n code\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% capture bulk_operation_query %}\n query {\n productVariants {\n edges {\n node {\n id\n inventoryItem {\n id\n inventoryLevels {\n edges {\n node {\n __typename\n id\n quantities(names: \"available\") {\n name\n quantity\n }\n location {\n legacyResourceId\n }\n }\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 {\"id\":\"gid:\\/\\/shopify\\/ProductVariant\\/1234567890\"}\n {\"__typename\":\"InventoryLevel\",\"quantities\":[{\"name\":\"available\",\"quantity\":20}],\"location\":{\"legacyResourceId\":{{ location_ids_and_metafield_keys.first.first | json }}},\"__parentId\":\"gid:\\/\\/shopify\\/ProductVariant\\/1234567890\"}\n {% endcapture %}\n\n {% assign bulkOperation = hash %}\n {% assign bulkOperation[\"objects\"] = jsonl_string | parse_jsonl %}\n {% endif %}\n\n {% assign inventory_levels = bulkOperation.objects | where: \"__typename\", \"InventoryLevel\" %}\n\n {% assign metafields = array %}\n\n {% for inventory_level in inventory_levels %}\n {% assign metafield_key = location_ids_and_metafield_keys[inventory_level.location.legacyResourceId] %}\n\n {% if metafield_key != blank and inventory_level.quantities.first.quantity != blank %}\n {% capture metafield %}\n {\n ownerId: {{ inventory_level.__parentId | json }}\n namespace: {{ metafield_namespace | json }}\n key: {{ metafield_key | json }}\n type: \"number_integer\"\n value: {{ inventory_level.quantities.first.quantity | append: \"\" | json }}\n }\n {% endcapture %}\n\n {% assign metafields = metafields | push: metafield %}\n {% endif %}\n {% endfor %}\n\n {% assign groups_of_metafields = metafields | in_groups_of: 25, fill_with: false %}\n\n {% for group_of_metafields in groups_of_metafields %}\n {% action \"shopify\" %}\n mutation {\n metafieldsSet(\n metafields: [\n {{ group_of_metafields | join: newline }}\n ]\n ) {\n metafields {\n id\n namespace\n key\n type\n value\n owner {\n ... on ProductVariant {\n id\n }\n }\n }\n userErrors {\n code\n field\n message\n }\n }\n }\n {% endaction %}\n {% endfor %}\n{% endif %}\n",
"subscriptions": [
"shopify/inventory_levels/connect",
"shopify/inventory_levels/update",
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation"
],
"subscriptions_template": "shopify/inventory_levels/connect\r\nshopify/inventory_levels/update\r\nmechanic/user/trigger\r\nmechanic/shopify/bulk_operation",
"tags": [
"Inventory",
"Metafields",
"Products",
"Sync",
"Variants"
]
}