Accept a maximum number of orders per day
This task works by setting your inventory to zero when the order limit is reached. (Specifically, this means setting inventory levels to 0 for all items that have a greater-than-zero inventory level.) When the limit is reached, your inventory will be dropped to zero. If your store is configured to stop selling out-of-stock products, your customers will be prevented from making additional purchases.
Try this taskWhen it runs
- Occurs whenever an order is created
- Occurs every day at midnight (in local time)
What you choose
- Maximum daily orders
- Only count orders matching this query
- Only clear inventory for products with this tag
- Restore inventory levels the next day
- Restore inventory levels on demand
Documentation
This task works by setting your inventory to zero when the order limit is reached. (Specifically, this means setting inventory levels to 0 for all items that have a greater-than-zero inventory level.) When the limit is reached, your inventory will be dropped to zero. If your store is configured to stop selling out-of-stock products, your customers will be prevented from making additional purchases.
And, this task can restore inventory to its original levels at midnight the next day, or on demand. (Restore levels on demand by enabling this option, then using the "Run task" button.).
Important notes
- This task only works for products that have inventory tracking enabled, which are configured to be unavailable when out of stock. This means that the "Track quantity" product option needs to be enabled, and the "Continue selling when out of stock" option needs to be disabled, for all products.
- The "Only clear inventory for products with this tag" does not change which orders are considered for the daily maximum. Use the "Only count orders matching this query" option to filter orders for counting, using the same order search syntax as the Shopify admin.
- The "Only clear inventory for products with this tag" setting is case-sensitive - it must match product tags exactly.
- This task may be not behave as intended if you receive multiple orders per minute. We don't recommend using it for high-volume stores.
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
{% if options.restore_inventory_levels_the_next_day__boolean %}
mechanic/scheduler/daily
{% endif %}
{% if options.restore_inventory_levels_on_demand__boolean %}
mechanic/user/trigger
{% endif %}
How task subscriptions work
Option types and flags
- Maximum daily orders (number, required)
- Restore inventory levels the next day (boolean)
- Restore inventory levels on demand (boolean)
Task code
{% comment %}
Options order:
{{ options.maximum_daily_orders__number_required }}
{{ options.only_count_orders_matching_this_query }}
{{ options.only_clear_inventory_for_products_with_this_tag }}
{{ options.restore_inventory_levels_the_next_day__boolean }}
{{ options.restore_inventory_levels_on_demand__boolean }}
{% endcomment %}
{% if options.maximum_daily_orders__number_required <= 0 %}
{% error "'Maximum daily orders' must be at least 1. :)" %}
{% endif %}
{% assign inventory_is_zeroed_cache_key = "inventory_is_zeroed:" | append: task.id %}
{% assign inventory_is_zeroed = cache[inventory_is_zeroed_cache_key] | default: false %}
{% if event.topic contains "shopify/orders" %}
{% assign cursor = nil %}
{% assign orders_today = 0 %}
{% assign previous_midnight = "now" | date: "%Y-%m-%dT00:00:00%z" %}
{% assign previous_midnight_s = previous_midnight | date: "%s" %}
{% assign base_cache_key = "inventory_to_restore:" | append: previous_midnight_s %}
{% capture orders_query %}
created_at:>={{ previous_midnight | json }} {{ options.only_count_orders_matching_this_query }}
{% endcapture %}
{% assign orders_query = orders_query | strip %}
{% for n in (0..100) %}
{% capture query %}
query {
orders(
first: 250
after: {{ cursor | json }}
query: {{ orders_query | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% assign inventory_is_zeroed = false %}
{% capture result_json %}
{
"data": {
"orders": {
"nodes": [
{% for n in (1..options.maximum_daily_orders__number_required) %}
{
"id": "gid://shopify/Order/1234567890"
}{% unless forloop.last %},{% endunless %}
{% endfor %}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign orders_today = orders_today | plus: result.data.orders.nodes.size %}
{% if result.data.orders.pageInfo.hasNextPage %}
{% assign cursor = result.data.orders.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% log orders_today_thus_far: orders_today, orders_query: orders_query, inventory_is_already_zeroed: inventory_is_zeroed %}
{% if orders_today >= options.maximum_daily_orders__number_required and inventory_is_zeroed == false %}
{% action "cache", "set", inventory_is_zeroed_cache_key, true %}
{% assign cursor = nil %}
{% assign inventory_adjustments = array %}
{% for n in (0..2000) %}
{% capture query %}
query {
inventoryItems(
first: 3
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
variant {
product {
tags
}
}
inventoryLevels(first: 100) {
nodes {
id
quantities(names: "available") {
name
quantity
}
location {
id
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"inventoryItems": {
"nodes": [
{
"id": "gid://shopify/InventoryItem/1234567890",
"variant": {
"product": {
"tags": [
{% if options.only_clear_inventory_for_products_with_this_tag != blank %}
{{ options.only_clear_inventory_for_products_with_this_tag | json }}
{% endif %}
]
}
},
"inventoryLevels": {
"nodes": [
{
"id": "gid://shopify/InventoryLevel/1234567890?inventory_item_id=1234567890",
"quantities": [
{
"name": "available",
"quantity": 20
}
],
"location": {
"id": "gid://shopify/Location/1234567890"
}
}
]
}
}
]
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% for inventory_item in result.data.inventoryItems.nodes %}
{% if options.only_clear_inventory_for_products_with_this_tag != blank %}
{% unless inventory_item.variant.product.tags contains options.only_clear_inventory_for_products_with_this_tag %}
{% continue %}
{% endunless %}
{% endif %}
{% for inventory_level in inventory_item.inventoryLevels.nodes %}
{% if inventory_level.quantities.first.quantity > 0 %}
{% comment %}
NOTE: use availble quantity in the hash to be stored in cache... however, a negative delta will be made in the mutation to zero out the inventory
{% endcomment %}
{% assign inventory_adjustment = hash %}
{% assign inventory_adjustment["inventoryItemId"] = inventory_item.id %}
{% assign inventory_adjustment["locationId"] = inventory_level.location.id %}
{% assign inventory_adjustment["delta"] = inventory_level.quantities.first.quantity %}
{% assign inventory_adjustments = inventory_adjustments | push: inventory_adjustment %}
{% endif %}
{% endfor %}
{% endfor %}
{% if result.data.inventoryItems.pageInfo.hasNextPage %}
{% assign cursor = result.data.inventoryItems.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% if inventory_adjustments == blank %}
{% break %}
{% endif %}
{% assign groups_of_inventory_adjustments = inventory_adjustments | in_groups_of: 250, fill_with: false %}
{% for group_of_inventory_adjustments in groups_of_inventory_adjustments %}
{% action "shopify" %}
mutation {
inventoryAdjustQuantities(
input: {
reason: "correction"
name: "available"
changes: [
{% for inventory_adjustment in group_of_inventory_adjustments -%}
{
inventoryItemId: {{ inventory_adjustment.inventoryItemId | json }}
locationId: {{ inventory_adjustment.locationId | json }}
delta: {{ inventory_adjustment.delta | times: -1 }}
}
{%- endfor %}
]
}
) {
inventoryAdjustmentGroup {
reason
changes {
name
delta
quantityAfterChange
item {
id
sku
}
location {
name
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% comment %}
-- store the inventory to restore in cache even if neither restore option is selected... just in case :)
{% endcomment %}
{% assign cache_key = base_cache_key | append: "_group" | append: forloop.index0 %}
{% assign cache_value = hash %}
{% assign cache_value["value"] = group_of_inventory_adjustments %}
{% unless forloop.last %}
{% assign next_cache_key = base_cache_key | append: "_group" | append: forloop.index %}
{% assign cache_value["next_key"] = next_cache_key %}
{% endunless %}
{% action "cache", "set", cache_key, cache_value %}
{% endfor %}
{% endif %}
{% elsif event.topic == "mechanic/scheduler/daily" or event.topic == "mechanic/user/trigger" %}
{% action "cache", "del", inventory_is_zeroed_cache_key %}
{% assign cache_keys = array %}
{% assign day_in_s = 60 | times: 60 | times: 24 %}
{% assign midnight = "now" | date: "%s" | minus: day_in_s | date: "%Y-%m-%dT00:00:00%z" %}
{% assign midnight_s = midnight | date: "%s" %}
{% assign cache_key = "inventory_to_restore:" | append: midnight_s | append: "_group0" %}
{% assign inventory_adjustments_cache_group = cache[cache_key] | default: hash %}
{% if inventory_adjustments_cache_group == blank %}
{% assign midnight = "now" | date: "%Y-%m-%dT00:00:00%z" %}
{% assign midnight_s = midnight | date: "%s" %}
{% assign cache_key = "inventory_to_restore:" | append: midnight_s | append: "_group0" %}
{% assign inventory_adjustments_cache_group = cache[cache_key] | default: hash %}
{% endif %}
{% if event.preview %}
{% assign inventory_adjustment = hash %}
{% assign inventory_adjustment["inventoryItemId"] = "gid://shopify/InventoryItem/1234567890" %}
{% assign inventory_adjustment["locationId"] = "gid://shopify/Location/1234567890" %}
{% assign inventory_adjustment["delta"] = 20 %}
{% assign inventory_adjustments_cache_group["value"] = array | push: inventory_adjustment %}
{% endif %}
{% if inventory_adjustments_cache_group == blank %}
{% error
message: "No cached inventory data was found!",
cache_key: cache_key
%}
{% break %}
{% else %}
{% for n in (0..1000) %}
{% action "shopify" %}
mutation {
inventoryAdjustQuantities(
input: {
reason: "correction"
name: "available"
changes: {{ inventory_adjustments_cache_group.value | graphql_arguments }}
}
) {
inventoryAdjustmentGroup {
reason
changes {
name
delta
quantityAfterChange
item {
id
sku
}
location {
name
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% assign cache_keys = cache_keys | push: cache_key %}
{% if inventory_adjustments_cache_group.next_key %}
{% assign cache_key = inventory_adjustments_cache_group.next_key %}
{% assign inventory_adjustments_cache_group = cache[cache_key] %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% for cache_key in cache_keys %}
{% action "cache", "del", cache_key %}
{% endfor %}
{% endif %}
{% endif %}
Mechanic Liquid reference
Default option values
- Maximum daily orders
10- Only count orders matching this query
-status:cancelled- Restore inventory levels the next day
true- Restore inventory levels on demand
false