Mechanic is a development and ecommerce automation platform for Shopify. :)
Use this task to tag orders as they come in, based on which sales channel created them. Run this task manually to backfill tags for all orders.
Runs Occurs whenever an order is created, Occurs when a user manually triggers the task, and Occurs when a bulk operation is completed. Configuration includes sales channel names and tags.
Use this task to tag orders as they come in, based on which sales channel created them. Run this task manually to backfill tags for all orders.
Please note: this task works by sales channel, and as such does not apply to orders that are created by an app. It's sometimes difficult to distinguish whether a third-party is using a sales channel or an app; if you're having trouble with this task, try its companion, Auto-tag orders by app.
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?)
shopify/orders/create mechanic/user/trigger mechanic/shopify/bulk_operation
{% assign sales_channel_names_and_tags = options.sales_channel_names_and_tags__keyval_required %} {% assign sales_channel_names = sales_channel_names_and_tags | keys %} {% comment %} -- get all of the sales channel names (i.e. publications aka app catalogs) in the shop {% endcomment %} {% capture query %} query { publications( first: 250 catalogType:APP ) { nodes { id catalog { ... on AppCatalog { apps(first: 1) { nodes { title } } } } } } } {% endcapture %} {% assign result = query | shopify %} {% if event.preview %} {% capture result_json %} { "data": { "publications": { "nodes": [ { "id": "gid://shopify/Publication/1234567890", "catalog": { "apps": { "nodes": [ { "title": {{ sales_channel_names.first | json }} } ] } } } ] } } } {% endcapture %} {% assign result = result_json | parse_json %} {% endif %} {% comment %} -- add the configured publication names into a hash for use in lookups {% endcomment %} {% assign publication_ids = array %} {% assign publication_names_by_id = hash %} {% for publication in result.data.publications.nodes %} {% assign publication_name = publication.catalog.apps.nodes.first.title %} {% if sales_channel_names contains publication_name %} {% assign publication_ids = publication_ids | push: publication.id %} {% assign publication_names_by_id[publication.id] = publication_name %} {% endif %} {% endfor %} {% if event.topic contains "shopify/orders/" %} {% capture query %} query { order(id: {{ order.admin_graphql_api_id | json }}) { id name tags publication { id } } } {% endcapture %} {% assign result = query | shopify %} {% assign orders = array | push: result.data.order %} {% elsif event.topic == "mechanic/user/trigger" %} {% capture bulk_operation_query %} query { orders { edges { node { __typename id name tags publication { id } } } } } {% endcapture %} {% action "shopify" %} mutation { bulkOperationRunQuery( query: {{ bulk_operation_query | json }} ) { bulkOperation { id status } userErrors { field message } } } {% endaction %} {% break %} {% elsif event.topic == "mechanic/shopify/bulk_operation" %} {% assign orders = bulkOperation.objects | where: "__typename", "Order" %} {% endif %} {% if event.preview %} {% capture orders_json %} [ { "id": "gid://shopify/Order/1234567890", "name": "#PREVIEW", "publication": { "id": {{ publication_ids.first | json }} } } ] {% endcapture %} {% assign orders = orders_json | parse_json %} {% endif %} {% comment %} -- process orders to see which should be tagged {% endcomment %} {% for order in orders %} {% assign order_publication_id = order.publication.id %} {% assign publication_name = publication_names_by_id[order_publication_id] %} {% if publication_name == blank %} {% log order_id: order.id, order_name: order.name, message: "No sales channel present for this order; it was probably created by an app (see https://usemechanic.com/task/auto-tag-orders-by-app)." %} {% continue %} {% endif %} {% assign tag_to_add = sales_channel_names_and_tags[publication_name] %} {% if tag_to_add == blank %} {% log order_id: order.id, order_name: order.name, message: "No tag configured for this channel. Skipping.", publication_name: publication_name, available_channels: available_channels %} {% elsif order.tags contains tag_to_add %} {% log order_id: order.id, order_name: order.name, message: "The order already has the tag for this channel. Skipping.", publication_name: publication_name, tag_to_add: tag_to_add %} {% else %} {% action "shopify" %} mutation { tagsAdd( id: {{ order.id | json }} tags: {{ tag_to_add | json }} ) { userErrors { field message } } } {% endaction %} {% endif %} {% endfor %}
{"Online Store" => "online-store", "Buy Button" => "buy-button"}