forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete-the-oldest-x-products-from-a-specific-collection.json
24 lines (24 loc) · 2.43 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\nThis task requires a collection ID - [learn how to find yours](https://help.usemechanic.com/en/articles/2946120-how-do-i-find-an-id-for-a-product-collection-order-or-something-else).",
"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 = shop.collections[options.collection_id__number_required] %}\n\n{% if event.preview != true and collection == nil %}\n {\"error\": {{ options.collection_id__number_required | json | prepend: \"Unable to find collection \" | json }}}\n{% endif %}\n\n{% assign products = collection.products %}\n\n{% if event.preview %}\n {% assign products = array %}\n {% assign products[0] = '{\"id\":12345,\"created_at\":\"2000-01-01\"}' | parse_json %}\n\n {% comment %}\n Uncomment me for more testing!\n {% assign products[1] = '{\"id\":67890,\"created_at\":\"2001-01-01\"}' | parse_json %}\n {% endcomment %}\n{% endif %}\n\n{% assign products_sorted = products | sort: \"created_at\" %}\n\n{% assign product_ids_to_delete = array %}\n{% for product in products_sorted %}\n {% if forloop.index > options.number_of_products_to_delete_at_once__number_required %}\n {% break %}\n {% endif %}\n\n {% assign product_ids_to_delete[product_ids_to_delete.size] = product.id %}\n{% endfor %}\n\n{% if options.test_mode__boolean %}\n {% action \"echo\" %}\n {\"product_ids_to_delete\": {{ product_ids_to_delete | json }}}\n {% endaction %}\n{% else %}\n {% for product_id in product_ids_to_delete %}\n {% action \"shopify\" %}\n [\n \"delete\",\n [\n \"product\",\n {{ product_id | json }}\n ]\n ]\n {% endaction %}\n {% endfor %}\n{% endif %}",
"subscriptions": [
"mechanic/user/trigger"
],
"subscriptions_template": "mechanic/user/trigger\n\n{% if options.run_daily__boolean %}\n mechanic/scheduler/daily\n{% endif %}",
"tags": [
"Collections",
"Delete",
"Products"
]
}