-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathdelete-the-oldest-x-products-from-a-specific-collection.json
24 lines (24 loc) · 3.64 KB
/
delete-the-oldest-x-products-from-a-specific-collection.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
{
"docs": "Either triggered manually, or configured to run daily, this task will look for the oldest products in the collection of your choice, and delete as many of them as you wish.\n\n**IMPORTANT**: When first configuring this task, run it manually once in \"Test mode\" to see a list of which products it would delete. Once verfied, be sure to uncheck this option to have the task make the deletions going forward.\n\n*Notes:*\n- The products will be deleted asycnronously by Shopify, which means they may appear in the products list of admin for a bit after a task run.\n- This task requires a collection ID - [learn how to locate common resource IDs](https://learn.mechanic.dev/techniques/finding-a-resource-id).",
"halt_action_run_sequence_on_error": false,
"name": "Delete the oldest x products from a specific collection",
"online_store_javascript": null,
"options": {
"collection_id__number_required": null,
"number_of_products_to_delete_at_once__number_required": null,
"test_mode__boolean": null,
"run_daily__boolean": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% assign collection_id = options.collection_id__number_required %}\n{% assign number_of_products_to_delete_at_once = options.number_of_products_to_delete_at_once__number_required %}\n{% assign test_mode = options.test_mode__boolean %}\n\n{% if number_of_products_to_delete_at_once > 250 %}\n {% error \"This task only supports deleting up to 250 products in a single task run.\" %}\n{% endif %}\n\n{% comment %}\n -- get X oldest products that are in the collection\n{% endcomment %}\n\n{% capture query %}\n query {\n collection(id: {{ collection_id | prepend: \"gid://shopify/Collection/\" | json }}) {\n products(\n first: {{ number_of_products_to_delete_at_once }}\n sortKey: CREATED\n ) {\n nodes {\n id\n title\n createdAt\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 \"collection\": {\n \"products\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/Product/1234567890\",\n \"createdAt\": \"2020-02-02T01:01:00Z\"\n }\n ]\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n{% endif %}\n\n{% assign collection = result.data.collection %}\n{% assign products = collection.products.nodes %}\n\n{% if collection == blank %}\n {% error \"Unable to find a collection by the configured ID in this shop.\" %}\n{% endif %}\n\n{% log products_to_delete: products %}\n\n{% if test_mode %}\n {% break %}\n{% endif %}\n\n{% comment %}\n -- delete all returned products asynchronously in case they have many variants and/or media (which could cause an API timeout)\n{% endcomment %}\n\n{% for product in collection.products.nodes %}\n {% action \"shopify\" %}\n mutation {\n productDelete(\n input: {\n id: {{ product.id | json }}\n }\n synchronous: false\n ) {\n productDeleteOperation {\n id\n product {\n legacyResourceId\n title\n }\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n{% endfor %}\n",
"subscriptions": [
"mechanic/user/trigger"
],
"subscriptions_template": "mechanic/user/trigger\n{% if options.run_daily__boolean %}\n mechanic/scheduler/daily\n{% endif %}",
"tags": [
"Collections",
"Delete",
"Products"
]
}