Mechanic is a development and ecommerce automation platform for Shopify. :)
This task will run daily to scan order timeline events from the past week to see if any orders have new shipping provider charges for labels. Qualifying orders will be tagged with the tag of your choice.
Runs Occurs every day at midnight (in local time) and Occurs when a user manually triggers the task. Configuration includes order tag to add.
This task will run daily to scan order timeline events from the past week to see if any orders have new shipping provider charges for labels. Qualifying orders will be tagged with the tag of your choice.
Run this task manually to scan the last 25K related order events. Note: You may need to enable read all orders to successfully tag qualifying orders older than 60 days.
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/scheduler/daily mechanic/user/trigger
{% assign order_tag_to_add = options.order_tag_to_add__required %}
{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
{%- capture search_query -%}
subject_type:ORDER action:shipping_label_adjustment_charge_created
{%- endcapture -%}
{% if event.topic contains "mechanic/scheduler/" %}
{%- capture search_query -%}
{{ search_query }} created_at:past_week
{%- endcapture -%}
{% endif %}
{% comment %}
-- get order timeline events related to shipping
-- limit daily runs to a lookback of the past week; manual runs are only restricted by the number of events (25K max for this paginated query)
{% endcomment %}
{% assign cursor = nil %}
{% assign events = array %}
{% for n in (1..100) %}
{% capture query %}
query {
events(
first: 250
reverse: true
sortKey: CREATED_AT
after: {{ cursor | json }}
query: {{ search_query | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
... on BasicEvent {
id
action
createdAt
message
subject {
... on Order {
id
name
createdAt
tags
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"events": {
"nodes": [
{
"id": "gid://shopify/BasicEvent/1234567890",
"action": "shipping_label_adjustment_charge_created",
"message": "You were charged $1.50 for a shipping label price adjustment.",
"subject": {
"id": "gid://shopify/Order/1234567890",
"name": "#PREVIEW"
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign events = events | concat: result.data.events.nodes %}
{% if result.data.events.pageInfo.hasNextPage %}
{% assign cursor = result.data.events.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% comment %}
-- tag all orders associated with shipping label price adjustment events, unless they are already tagged
{% endcomment %}
{% for event in events %}
{% assign order = event.subject %}
{% if order == blank %}
{% log
message: "No order object was returned with this event. Check to make sure that Mechanic has access to order older than 60 days.",
event: event
%}
{% continue %}
{% endif %}
{% unless order.tags contains order_tag_to_add %}
{% log
message: "Order qualified to be tagged",
event: event
%}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order.id | json }}
tags: {{ order_tag_to_add | json }}
) {
userErrors {
field
message
}
}
}
{% endaction %}
{% endunless %}
{% endfor %}
{% endif %}
shipping-label-price-adjustment