Delete non-purchased Neon Sign Customiser app generated products older than X days
Use this task to automatically delete non-purchased products created by Neon Sign Customiser. Set the number of days old products should be before Mechanic deletes a product.
Try this taskWhen it runs
Occurs when a user manually triggers the task
What you choose
- Products must be x number of days old
- Test mode
- Live mode
- Run daily
Documentation
Use this task to automatically delete non-purchased products created by Neon Sign Customiser. Set the number of days old products should be before Mechanic deletes a product.
Tick the "Test mode" checkbox to verify which products will be deleted: after saving, use the "Run task" button to run the product scan, and you'll receive a list of products that Mechanic would have deleted in live mode.
Once you're ready, uncheck the "Test mode" box and check the "Live mode" box, save the task, then use "Run task" to permanently delete all matching products.
Check the run daily option to have the task run at 12AM daily.
Task code and subscriptions
You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.
Subscriptions
mechanic/user/trigger
{% if options.run_daily__boolean %}
mechanic/scheduler/daily
{% endif %}
How task subscriptions work
Option types and flags
- Products must be x number of days old (number, required)
- Test mode (boolean)
- Live mode (boolean)
- Run daily (boolean)
Task code
{% comment %}
Options ordering:
{{ options.products_must_be_x_number_of_days_old__number__required }}
{{ options.test_mode__boolean }}
{{ options.live_mode__boolean }}
{% endcomment %}
{% assign products_must_be_x_number_of_seconds_old = options.products_must_be_x_number_of_days_old__number__required | times: 24 | times: 60 | times: 60 %}
{% assign products_age_threshold_s = "now" | date: "%s" | minus: products_must_be_x_number_of_seconds_old %}
{% assign products_age_threshold_human = products_age_threshold_s | date: "%Y-%m-%d %H:%M %:z" %}
{% assign products_age_threshold_date = products_age_threshold_s | date: "%Y-%m-%d" %}
{% log %}{{ "Threshold for deleteing products: " | append: products_age_threshold_human | json }}{% endlog %}
{% assign product_ids_and_titles = hash %}
{% capture product_query -%}
-tag:npc_ordered product_type:'Custom Neon' created_at:<{{ products_age_threshold_date }}
{% endcapture %}
{% assign cursor = nil %}
{% for n in (0..100) %}
{% capture query %}
query {
products(
first: 250
after: {{ cursor | json }}
sortKey: TITLE
query: {{ product_query | json }}
) {
pageInfo {
hasNextPage
}
edges {
cursor
node {
id
title
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"products": {
"pageInfo": {
"hasNextPage": false
},
"edges": [
{
"node": {
"id": "gid://shopify/Product/1234567890",
"title": "[sample product]"
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% for product_edge in result.data.products.edges %}
{% assign product_ids_and_titles[product_edge.node.id] = product_edge.node.title %}
{% endfor %}
{% if result.data.products.pageInfo.hasNextPage %}
{% assign cursor = result.data.products.edges.last.cursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% if options.test_mode__boolean and options.live_mode__boolean %}
{% error "Please choose either test mode or live mode." %}
{% elsif options.test_mode__boolean == false and options.live_mode__boolean == false %}
{% error "Please choose either test mode or live mode." %}
{% elsif options.test_mode__boolean %}
{% log products_found_count: product_ids_and_titles.size, products_found: product_ids_and_titles %}
{% elsif options.live_mode__boolean %}
{% for keyval in product_ids_and_titles %}
{% action "shopify" %}
mutation {
productDelete(
input: {
id: {{ keyval[0] | json }}
}
) {
deletedProductId
userErrors {
field
message
}
}
}
{% endaction %}
{% endfor %}
{% endif %}
Mechanic Liquid reference
Default option values
- Products must be x number of days old
7- Test mode
true- Live mode
false- Run daily
false