Auto-tag orders that contain an out of stock item
This task watches for new 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.
Try this taskWhen it runs
Occurs whenever an order is created
What you choose
- Apply this order tag
- Ignore variants marked for oversell
- Apply this customer tag
- Apply this product tag for each out of stock line item
Documentation
This task watches for new 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.
By default, this task will disregard the inventory levels of any variants which have the "Continue selling when out of stock" option enabled. Uncheck the "Ignore variants marked for oversell" task option to disable this exclusion.
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
shopify/orders/create
How task subscriptions work
Option types and flags
- Apply this order tag (required)
- Ignore variants marked for oversell (boolean)
Task code
{% assign apply_this_order_tag = options.apply_this_order_tag__required %}
{% assign ignore_variants_marked_for_oversell = options.ignore_variants_marked_for_oversell__boolean %}
{% assign apply_this_customer_tag = options.apply_this_customer_tag %}
{% assign apply_this_product_tag = options.apply_this_product_tag_for_each_out_of_stock_line_item %}
{% comment %}
-- get product and variant data for all line items on this order
{% endcomment %}
{% assign out_of_stock_products = array %}
{% capture query %}
query {
order(id: {{ order.admin_graphql_api_id | json }}) {
id
tags
{% if apply_this_customer_tag != blank %}
customer {
id
tags
}
{% endif %}
lineItems(first: 250) {
nodes {
name
product {
id
title
tags
tracksInventory
}
variant {
inventoryQuantity
inventoryPolicy
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"order": {
"id": "gid://shopify/Order/1234567890",
"customer": {
"id": "gid://shopify/Customer/1234567890"
},
"lineItems": {
"nodes": [
{
"name": "An out of stock product",
"product": {
"id": "gid://shopify/Product/1234567890",
"title": "An out of stock product",
"tracksInventory" : true
},
"variant": {
"inventoryQuantity": -1,
"inventoryPolicy": "DENY"
}
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign order = result.data.order %}
{% for line_item in order.lineItems.nodes %}
{% assign product = line_item.product %}
{% assign variant = line_item.variant %}
{% comment %}
-- skip custom line items (i.e. not attached to a product in the shop), and products that do not track inventory
{% endcomment %}
{% if product == blank %}
{% continue %}
{% endif %}
{% unless product.tracksInventory %}
{% continue %}
{% endunless %}
{% if variant.inventoryQuantity < 0 %}
{% comment %}
-- ignore variants with negative inventory if they are set to oversell and that option is enabled in the task
{% endcomment %}
{% if ignore_variants_marked_for_oversell and variant.inventoryPolicy == "CONTINUE" %}
{%- capture log_output -%}
{{ line_item.name }} was purchased while at stock level {{ variant.inventoryQuantity }}; however, the variant's inventory policy allows overselling and the "Ignore variants marked for oversell" task option is enabled; skipping.
{%- endcapture -%}
{% log log_output %}
{% continue %}
{% endif %}
{% assign out_of_stock_products = out_of_stock_products | push: product | uniq %}
{% endif %}
{% endfor %}
{% if out_of_stock_products == blank %}
{% log "No out of stock products for this order." %}
{% break %}
{% endif %}
{% comment %}
-- apply the order tag
{% endcomment %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order.id | json }}
tags: {{ apply_this_order_tag | json }}
) {
node {
... on Order {
id
name
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% comment %}
-- apply the customer tag if it is configured, there is a customer for the order, and they do not yet have the tag
{% endcomment %}
{% unless apply_this_customer_tag == blank or order.customer == blank %}
{% if order.customer.tags contains apply_this_customer_tag %}
{% log "This customer already has the configured tag." %}
{% else %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ order.customer.id | json }}
tags: {{ apply_this_customer_tag | json }}
) {
node {
... on Customer {
id
displayName
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endunless %}
{% comment %}
-- apply product tag if it is configured, for any products that do not yet have the tag
{% endcomment %}
{% if apply_this_product_tag != blank %}
{% for product in out_of_stock_products %}
{% if product.tags contains apply_this_product_tag %}
{%- capture log_output -%}
The product "{{ product.title }}" already has the configured tag.
{%- endcapture -%}
{% log log_output %}
{% else %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ product.id | json }}
tags: {{ apply_this_product_tag | json }}
) {
node {
... on Product {
id
title
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}
{% endif %}
Mechanic Liquid reference
Default option values
- Ignore variants marked for oversell
true