Mechanic is a development and ecommerce automation platform for Shopify. :)
Keep your draft order list tidy - use this task to automatically delete orders that are older than a configurable number of days.
Runs Occurs when a user manually triggers the task. Configuration includes days to wait before deleting a draft order, test mode, and run daily.
Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, everybody.
That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.
(By the way, have you seen our documentation? Have you joined the Slack community?)
mechanic/user/trigger {% if options.run_daily__boolean %}mechanic/scheduler/daily{% endif %}
{% assign now_s = "now" | date: "%s" | times: 1 %} {% assign x_days_ago_interval_s = options.days_to_wait_before_deleting_a_draft_order__number_required | times: 24 | times: 60 | times: 60 %} {% assign x_days_ago_s = now_s | minus: x_days_ago_interval_s %} {% assign x_days_ago_date = x_days_ago_s | date: "%Y-%m-%d" %} {% assign ids_to_delete = array %} {% assign cursor = nil %} {% for n in (0..100) %} {% capture query %} query { draftOrders( first: 250 after: {{ cursor | json }} query: {{ "created_at:<" | append: x_days_ago_date | json }} sortKey: ID ) { edges { cursor node { id name createdAt } } } } {% endcapture %} {% assign result = query | shopify %} {% for draftOrder_edge in result.data.draftOrders.edges %} {% assign draftOrder = draftOrder_edge.node %} {% assign ids_to_delete[ids_to_delete.size] = draftOrder.id %} {% endfor %} {% if result.data.draftOrders.pageInfo.hasNextPage %} {% assign cursor = result.data.draftOrders.edges.last.cursor %} {% else %} {% break %} {% endif %} {% endfor %} {% if event.preview %} {% assign ids_to_delete[0] = "gid://shopify/DraftOrder/1234567890" %} {% endif %} {% if options.test_mode__boolean %} {% capture message -%} Test mode: found {{ ids_to_delete.size }} qualifying draft order(s). Disable test mode and run again to delete draft orders that qualify during that run. {%- endcapture %} {% action "echo" message %} {% else %} {% for id in ids_to_delete %} {% action "shopify" %} mutation { draftOrderDelete( input: { id: {{ id | json }} } ) { userErrors { field message } } } {% endaction %} {% endfor %} {% endif %}
30
true