forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.liquid
46 lines (38 loc) · 1.32 KB
/
script.liquid
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{% assign collection = shop.collections[options.collection_id__number_required] %}
{% if event.preview != true and collection == nil %}
{"error": {{ options.collection_id__number_required | json | prepend: "Unable to find collection " | json }}}
{% endif %}
{% assign products = collection.products %}
{% if event.preview %}
{% assign products = array %}
{% assign products[0] = '{"id":12345,"created_at":"2000-01-01"}' | parse_json %}
{% comment %}
Uncomment me for more testing!
{% assign products[1] = '{"id":67890,"created_at":"2001-01-01"}' | parse_json %}
{% endcomment %}
{% endif %}
{% assign products_sorted = products | sort: "created_at" %}
{% assign product_ids_to_delete = array %}
{% for product in products_sorted %}
{% if forloop.index > options.number_of_products_to_delete_at_once__number_required %}
{% break %}
{% endif %}
{% assign product_ids_to_delete[product_ids_to_delete.size] = product.id %}
{% endfor %}
{% if options.test_mode__boolean %}
{% action "echo" %}
{"product_ids_to_delete": {{ product_ids_to_delete | json }}}
{% endaction %}
{% else %}
{% for product_id in product_ids_to_delete %}
{% action "shopify" %}
[
"delete",
[
"product",
{{ product_id | json }}
]
]
{% endaction %}
{% endfor %}
{% endif %}