forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-associate-variants-with-a-delivery-profile-by-metafield-value.json
25 lines (25 loc) · 6.52 KB
/
auto-associate-variants-with-a-delivery-profile-by-metafield-value.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
{
"docs": "Use this task to automatically add variants to the configured delivery profile on product update, if any of the variants have a metafield value matching the configured value. Conversely, variants with a non-existant or mismatched metafield value that are in the configured delivery profile will be removed.\n\nConfigure this task with a variant metafield namespace and key separated by a period (e.g. \"custom.my_metafield\"), the metafield value to match, and the delivery profile ID. Find the delivery profile ID by navigating to the \"Shipping and delivery\" section of your Shopify settings, and clicking on the \"Manage \" link of the delivery profile you want to use. The delivery profile ID is the series of numbers at the very end of the URL – if the URL is admin.shopify.com/store/{your_shop}/settings/shipping/profiles/12345, then the delivery profile ID is 123545.\n\nAs an initial setup, you can run the task manually to have it review all of the variants in your shop.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-associate variants with a delivery profile, by metafield value",
"online_store_javascript": null,
"options": {
"metafield_namespace_and_key__required": null,
"metafield_value_to_match__required": null,
"delivery_profile_id__required_number": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign metafield_namespace_and_key = options.metafield_namespace_and_key__required %}\n{% assign metafield_value_to_match = options.metafield_value_to_match__required %}\n{% assign delivery_profile_id = options.delivery_profile_id__required_number %}\n\n{% capture query %}\n query {\n deliveryProfile(id: {{ \"gid://shopify/DeliveryProfile/\" | append: delivery_profile_id | json }}) {\n name\n id\n }\n }\n{% endcapture %}\n\n{% assign result = query | shopify %}\n\n{% assign delivery_profile = result.data.deliveryProfile %}\n\n{% unless delivery_profile or event.preview %}\n {% error \"Delivery profile not found! Please double-check the profile ID.\" %}\n {% break %}\n{% endunless %}\n\n{% if event.topic == \"shopify/products/update\" %}\n {% capture query %}\n query {\n product(id: {{ product.admin_graphql_api_id | json }}) {\n variants(first: 100) {\n nodes {\n id\n deliveryProfile {\n id\n }\n metafield(\n key: {{ metafield_namespace_and_key | json }}\n ) {\n value\n }\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% assign variants = result.data.product.variants.nodes %}\n\n{% elsif event.topic == \"mechanic/user/trigger\" %}\n {% assign cursor = nil %} \n {% assign variants = array %}\n \n {% for n in (1..200) %}\n {% capture query %}\n query {\n productVariants(\n first: 250\n after: {{ cursor | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n nodes {\n id\n deliveryProfile {\n id\n }\n metafield(\n key: {{ metafield_namespace_and_key | json }}\n ) {\n value\n }\n }\n }\n }\n {% endcapture %}\n \n {% assign result = query | shopify %}\n \n {% assign variants\n = result.data.productVariants.nodes\n | default: array\n | concat: variants\n %}\n \n {% if result.data.productVariants.pageInfo.hasNextPage %}\n {% assign cursor = result.data.productVariants.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n{% endif %}\n\n{% if event.preview %}\n {% capture variants_json %}\n [\n {\n \"id\": \"gid://shopify/ProductVariant/1234567890\",\n \"deliveryProfile\": {\n \"id\": \"gid://shopify/DeliveryProfile/1234567890\"\n },\n \"metafield\": {\n \"value\": {{ metafield_value_to_match | json }}\n }\n }\n ]\n {% endcapture %}\n\n {% assign variants = variants_json | parse_json %}\n{% endif %}\n\n{% assign variant_actions_and_ids = array %}\n\n{% for variant in variants %}\n {% unless variant.deliveryProfile %}\n {% comment %}\n -- digital or other type of product that does not require shipping; skip it\n {% endcomment %}\n {% continue %}\n {% endunless%}\n\n {% if variant.metafield.value == metafield_value_to_match %}\n {% if variant.deliveryProfile.id != delivery_profile.id %}\n {% assign variant_action_and_id = hash %}\n {% assign variant_action_and_id[\"action\"] = \"associate\" %}\n {% assign variant_action_and_id[\"id\"] = variant.id %}\n {% assign variant_actions_and_ids = variant_actions_and_ids | push: variant_action_and_id %}\n {% endif %}\n\n {% elsif variant.deliveryProfile.id == delivery_profile.id %}\n {% assign variant_action_and_id = hash %}\n {% assign variant_action_and_id[\"action\"] = \"dissociate\" %}\n {% assign variant_action_and_id[\"id\"] = variant.id %}\n {% assign variant_actions_and_ids = variant_actions_and_ids | push: variant_action_and_id %}\n {% endif %}\n{% endfor %}\n\n{% assign groups_of_variant_actions_and_ids = variant_actions_and_ids | in_groups_of: 250, fill_with: false %}\n\n{% for group_of_variant_actions_and_ids in groups_of_variant_actions_and_ids %}\n {% assign variant_ids_to_associate\n = group_of_variant_actions_and_ids\n | where: \"action\", \"associate\"\n | map: \"id\"\n %}\n {% assign variant_ids_to_dissociate\n = group_of_variant_actions_and_ids\n | where: \"action\", \"dissociate\"\n | map: \"id\"\n %}\n\n {% action \"shopify\" %}\n mutation {\n deliveryProfileUpdate(\n id: {{ delivery_profile.id | json }}\n profile: {\n variantsToAssociate: {{ variant_ids_to_associate | json }}\n variantsToDissociate: {{ variant_ids_to_dissociate | json }}\n }\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n\n{% else %}\n {% log \n message: \"No variants qualified to be added or removed from the configured delivery profile in this task run.\",\n task_options_for_this_run: task.options\n %}\n{% endfor %}",
"subscriptions": [
"shopify/products/update",
"mechanic/user/trigger"
],
"subscriptions_template": "shopify/products/update\nmechanic/user/trigger",
"tags": [
"Auto-Update",
"Delivery",
"Metafields",
"Variants"
]
}