Accept a maximum number of orders per day, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

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.

Runs Occurs whenever an order is created and Occurs every day at midnight (in local time). Configuration includes maximum daily orders, only count orders matching this query, only clear inventory for products with this tag, restore inventory levels the next day, and restore inventory levels on demand.

15-day free trial – unlimited tasks

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.

Developer details

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?)

Open source
View on GitHub to contribute to this task
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 %}
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
maximum daily orders (number, required) , only count orders matching this query , only clear inventory for products with this tag , restore inventory levels the next day (boolean) , restore inventory levels on demand (boolean)
Code
{% assign maximum_daily_orders = options.maximum_daily_orders__number_required %}
{% assign only_count_orders_matching_this_query = options.only_count_orders_matching_this_query %}
{% assign only_clear_inventory_for_products_with_this_tag = options.only_clear_inventory_for_products_with_this_tag %}

{% if maximum_daily_orders <= 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 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 }} {{ only_count_orders_matching_this_query }}
  {%- endcapture -%}

  {% comment %}
    -- get count of orders created today
  {% endcomment %}

  {% capture query %}
    query {
      ordersCount(
        query: {{ orders_query | json }}
        limit: null
      ) {
        count
        precision
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "ordersCount": {
            "count": {{ maximum_daily_orders }}
          }
        }
      }
    {% endcapture %}

    {% assign result = result_json | parse_json %}
  {% endif %}

  {% assign orders_today = result.data.ordersCount.count %}

  {% log
    orders_today_thus_far: orders_today,
    orders_query: orders_query,
    inventory_is_already_zeroed: inventory_is_zeroed
  %}

  {% if orders_today < maximum_daily_orders or inventory_is_zeroed %}
    {% log "Today's order count has not yet reached the maximum, or the inventory has already been zeroed." %}
    {% break %}
  {% endif %}

  {% action "cache", "set", inventory_is_zeroed_cache_key, true %}

  {% assign cursor = nil %}
  {% assign inventory_adjustments = array %}
  {% assign reverse_inventory_adjustments = array %}

  {% for n in (1..100) %}
    {% capture query %}
      query {
        inventoryItems(
          first: 250
          after: {{ cursor | json }}
        ) {
          pageInfo {
            hasNextPage
            endCursor
          }
          nodes {
            id
            variants(first: 1) {
              nodes {
                product {
                  tags
                }
              }
            }
            inventoryLevels(first: 100) {
              nodes {
                id
                quantities(names: "available") {
                  quantity
                }
                location {
                  id
                }
              }
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "inventoryItems": {
              "nodes": [
                {
                  "id": "gid://shopify/InventoryItem/1234567890",
                  "variants": {
                    "nodes": [
                      {
                        "product": {
                          "tags": [
                            {% if only_clear_inventory_for_products_with_this_tag != blank %}
                              {{ only_clear_inventory_for_products_with_this_tag | json }}
                            {% endif %}
                          ]
                        }
                      }
                    ]
                  },
                  "inventoryLevels": {
                    "nodes": [
                      {
                        "id": "gid://shopify/InventoryLevel/1234567890?inventory_item_id=1234567890",
                        "quantities": [
                          {
                            "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 only_clear_inventory_for_products_with_this_tag != blank %}
        {% unless inventory_item.variants.nodes.first.product.tags contains 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 %}
          {% 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 | times: -1 %}
          {% assign inventory_adjustment["changeFromQuantity"] = inventory_level.quantities.first.quantity %}
          {% assign inventory_adjustments = inventory_adjustments | push: inventory_adjustment %}

          {% comment %}
            -- create reverse inventory adjustment to store in cache
            -- IMPORTANT: using a fixed value of 0 for the changeFromQuantity assumes that "zeroed" inventory is not adjusted in any way prior to being restored by this task. If the possibility of adjustment exists, then the changeFromQuantity value should be set to null
          {% endcomment %}

          {% assign reverse_inventory_adjustment = hash %}
          {% assign reverse_inventory_adjustment["inventoryItemId"] = inventory_item.id %}
          {% assign reverse_inventory_adjustment["locationId"] = inventory_level.location.id %}
          {% assign reverse_inventory_adjustment["delta"] = inventory_level.quantities.first.quantity %}
          {% assign reverse_inventory_adjustment["changeFromQuantity"] = 0 %}
          {% assign reverse_inventory_adjustments = reverse_inventory_adjustments | push: reverse_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 %}

  {% comment %}
    -- changeFromQuantity field is required as of 2026-04
    https://shopify.dev/changelog/making-changefromquantity-field-required
    -- Shopify will enforce idempotency for several mutations as of 2026-04
    https://shopify.dev/changelog/making-idempotency-mandatory-for-inventory-adjustments-and-refund-mutations
  {% endcomment %}

  {% assign groups_of_inventory_adjustments = inventory_adjustments | in_groups_of: 250, fill_with: false %}

  {% for group_of_inventory_adjustments in groups_of_inventory_adjustments %}
    {% capture idempotent_key %}
      {
        "task_id": {{ task.id | json }},
        "event_id": {{ event.id | json }},
        "data": {{ group_of_inventory_adjustments | json }}
      }
    {% endcapture %}

    {% action "shopify" %}
      mutation {
        inventoryAdjustQuantities(
          input: {
            reason: "correction"
            name: "available"
            changes: {{ group_of_inventory_adjustments | graphql_arguments }}
          }
        ) @idempotent(key: {{ idempotent_key | json | md5 | json }}) {
          inventoryAdjustmentGroup {
            reason
            changes(quantityNames: "available") {
              name
              delta
              item {
                id
                sku
              }
              location {
                name
              }
            }
          }
          userErrors {
            code
            field
            message
          }
        }
      }
    {% endaction %}
  {% endfor %}

  {% assign groups_of_reverse_inventory_adjustments = reverse_inventory_adjustments | in_groups_of: 250, fill_with: false %}

  {% comment %}
    -- cache the reverse inventory adjustments even if neither restore option is selected... just in case :)
  {% endcomment %}

  {% for group_of_reverse_inventory_adjustments in groups_of_reverse_inventory_adjustments %}
    {% assign cache_key = base_cache_key | append: "_group" | append: forloop.index0 %}
    {% assign cache_value = hash %}
    {% assign cache_value["value"] = group_of_reverse_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 %}

{% elsif event.topic == "mechanic/scheduler/daily" or event.topic == "mechanic/user/trigger" %}
  {% comment %}
    -- reset inventory levels if they are cached
  {% endcomment %}

  {% 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_adjustment["changeFromQuantity"] = 0 %}
    {% assign inventory_adjustments_cache_group["value"] = array | push: inventory_adjustment %}
  {% endif %}

  {% comment %}
    -- changeFromQuantity field is required as of 2026-04
    https://shopify.dev/changelog/making-changefromquantity-field-required
    -- Shopify will enforce idempotency for several mutations as of 2026-04
    https://shopify.dev/changelog/making-idempotency-mandatory-for-inventory-adjustments-and-refund-mutations
  {% endcomment %}

  {% if inventory_adjustments_cache_group == blank %}
    {% error
      message: "No cached inventory data was found!",
      cache_key: cache_key
    %}
    {% break %}
  {% endif %}

  {% for n in (1..1000) %}
    {% capture idempotent_key %}
      {
        "task_id": {{ task.id | json }},
        "event_id": {{ event.id | json }},
        "data": {{ inventory_adjustments_cache_group.value | json }}
      }
    {% endcapture %}

    {% action "shopify" %}
      mutation {
        inventoryAdjustQuantities(
          input: {
            reason: "correction"
            name: "available"
            changes: {{ inventory_adjustments_cache_group.value | graphql_arguments }}
          }
        ) @idempotent(key: {{ idempotent_key | json | md5 | json }}) {
          inventoryAdjustmentGroup {
            reason
            changes(quantityNames: "available") {
              name
              delta
              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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Maximum daily orders
10
Only count orders matching this query
-status:cancelled
Restore inventory levels the next day
true