Mechanic is a development and ecommerce automation platform for Shopify. :)
This task watches for newly-created orders, and checks each line item for that variant's total inventory quantity. If any are found with an inventory level of 0 or less, the task will add the tag of your choice to the order – and, optionally, will add a tag to the customer and to each product related to an out-of-stock line item.
Runs Occurs whenever an order is created. Configuration includes apply this order tag, apply this customer tag, and apply this product tag for each out of stock line item.
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
{% comment %} Preferred option order: {{ options.apply_this_order_tag__required }} {{ options.apply_this_customer_tag }} {{ options.apply_this_product_tag_for_each_out_of_stock_line_item }} {% endcomment %} {% assign cursor = nil %} {% assign out_of_stock_line_items = array %} {% for n in (0..100) %} {% capture query %} query { order(id: {{ order.admin_graphql_api_id | json }}) { id tags {% if options.apply_this_customer_tag != blank %} customer { id tags } {% endif %} lineItems( first: 250 after: {{ cursor | json }} ) { pageInfo { hasNextPage } edges { cursor node { name product { id tags tracksInventory } variant { inventoryQuantity inventoryPolicy } } } } } } {% endcapture %} {% assign result = query | shopify %} {% if event.preview %} {% capture result_json %} { "data": { "order": { "id": "gid://shopify/Order/1234567890", "tags": [], "customer": { "id": "gid://shopify/Customer/1234567890", "tags": [] }, "lineItems": { "pageInfo": { "hasNextPage": false }, "edges": [ { "cursor": "eyJsYXN0X2lkIjo0NTEwOTE5MzI3ODA1LCJsYXN0X3ZhbHVlIjo0NTEwOTE5MzI3ODA1fQ==", "node": { "name": "An out of stock product", "product": { "id": "gid://shopify/Product/1234567890", "tags": [], "tracksInventory" : true }, "variant": { "inventoryQuantity": -1, "inventoryPolicy": "DENY" } } } ] } } } } {% endcapture %} {% assign result = result_json | parse_json %} {% endif %} {% for line_item_edge in result.data.order.lineItems.edges %} {% assign variant = line_item_edge.node.variant %} {% if variant and variant.inventoryQuantity < 0 and variant.inventoryPolicy == "DENY" and line_item_edge.node.product.tracksInventory %} {% log %}{{ line_item_edge.node.name | append: " was purchased while at stock level " | append: variant.inventoryQuantity | json }}{% endlog %} {% assign out_of_stock_line_items[out_of_stock_line_items.size] = line_item_edge.node %} {% endif %} {% endfor %} {% if result.data.order.lineItems.pageInfo.hasNextPage %} {% assign cursor = result.data.order.lineItems.edges.last.cursor %} {% else %} {% break %} {% endif %} {% endfor %} {% assign mutations = array %} {% assign order = result.data.order %} {% if out_of_stock_line_items != empty %} {% if order.tags contains options.apply_this_order_tag__required %} {% log %}{{ "This order is already tagged " | append: options.apply_this_order_tag__required | json }}{% endlog %} {% else %} {% capture mutation %} order: tagsAdd( id: {{ order.id | json }} tags: {{ options.apply_this_order_tag__required | json }} ) { userErrors { field message } } {% endcapture %} {% assign mutations[mutations.size] = mutation %} {% endif %} {% if options.apply_this_customer_tag != blank %} {% if order.customer.tags contains options.apply_this_customer_tag %} {% log %}{{ "This customer is already tagged " | append: options.apply_this_customer_tag | json }}{% endlog %} {% else %} {% capture mutation %} customer: tagsAdd( id: {{ order.customer.id | json }} tags: {{ options.apply_this_customer_tag | json }} ) { userErrors { field message } } {% endcapture %} {% assign mutations[mutations.size] = mutation %} {% endif %} {% endif %} {% if options.apply_this_product_tag_for_each_out_of_stock_line_item != blank %} {% for line_item in out_of_stock_line_items %} {% if line_item.product.tags contains options.apply_this_product_tag_for_each_out_of_stock_line_item %} {% log %}{{ "The product for line item " | append: line_item.name | append: " is already tagged with " | append: options.apply_this_product_tag_for_each_out_of_stock_line_item | json }}{% endlog %} {% else %} {% capture mutation %} lineItem{{ forloop.index }}: tagsAdd( id: {{ line_item.product.id | json }} tags: {{ options.apply_this_product_tag_for_each_out_of_stock_line_item | json }} ) { userErrors { field message } } {% endcapture %} {% assign mutations[mutations.size] = mutation %} {% endif %} {% endfor %} {% endif %} {% else %} {% log "No out of stock line items for this order." %} {% endif %} {% if mutations != empty %} {% action "shopify" %} mutation { {{ mutations | join: newline }} } {% endaction %} {% endif %}