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 when an order is created and every day at midnight. 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, Gurus, 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.

Open source
View on GitHub to contribute to this task
Events
when an order is created (shopify/orders/create)
every day at midnight (mechanic/scheduler/daily)
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)
Script
{% 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 inventory_levels_to_zero = array %}

  {% 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
          }
          edges {
            cursor
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% assign inventory_is_zeroed = false %}

      {% capture result_json %}
        {
          "data": {
            "orders": {
              "pageInfo": {
                "hasNextPage": false
              },
              "edges": [
                {% for n in (1..options.maximum_daily_orders__number_required) %}
                  {
                    "cursor": "eyJsYXN0X2lkIjoyMTQ4MTQ2MjQ5NzczLCJsYXN0X3ZhbHVlIjoiMjAyMC0wNC0wMyAxNzowMjoxMyJ9"
                  }{% unless forloop.last %},{% endunless %}
                {% endfor %}
              ]
            }
          }
        }
      {% endcapture %}

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

    {% assign orders_today = orders_today | plus: result.data.orders.edges.size %}

    {% if result.data.orders.pageInfo.hasNextPage %}
      {% assign cursor = result.data.orders.edges.last.cursor %}
    {% 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 %}

    {% for n in (0..100) %}
      {% capture query %}
        query {
          inventoryItems(
            first: 65
            after: {{ cursor | json }}
          ) {
            pageInfo {
              hasNextPage
            }
            edges {
              cursor
              node {
                variant {
                  product {
                    tags
                  }
                }
                inventoryLevels(
                  first: 10
                ) {
                  edges {
                    node {
                      id
                      available
                    }
                  }
                }
              }
            }
          }
        }
      {% endcapture %}

      {% assign result = query | shopify %}

      {% if event.preview %}
        {% capture result_json %}
          {
            "data": {
              "inventoryItems": {
                "pageInfo": {
                  "hasNextPage": false
                },
                "edges": [
                  {
                    "cursor": "eyJsYXN0X2lkIjozNTIxODc4MTAxMjAxMywibGFzdF92YWx1ZSI6IjM1MjE4NzgxMDEyMDEzIn0=",
                    "node": {
                      "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": {
                        "edges": [
                          {
                            "node": {
                              "id": "gid://shopify/InventoryLevel/1234567890?inventory_item_id=1234567890",
                              "available": 20
                            }
                          }
                        ]
                      }
                    }
                  }
                ]
              }
            }
          }
        {% endcapture %}

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

      {% for inventoryItem_edge in result.data.inventoryItems.edges %}
        {% if options.only_clear_inventory_for_products_with_this_tag != blank %}
          {% unless inventoryItem_edge.node.variant.product.tags contains options.only_clear_inventory_for_products_with_this_tag %}
            {% continue %}
          {% endunless %}
        {% endif %}

        {% for inventoryLevel_edge in inventoryItem_edge.node.inventoryLevels.edges %}
          {% if inventoryLevel_edge.node.available <= 0 %}
            {% continue %}
          {% endif %}

          {% assign inventory_levels_to_zero[inventory_levels_to_zero.size] = inventoryLevel_edge.node %}
        {% endfor %}
      {% endfor %}

      {% if result.data.inventoryItems.pageInfo.hasNextPage %}
        {% assign cursor = result.data.inventoryItems.edges.last.cursor %}
      {% else %}
        {% break %}
      {% endif %}
    {% endfor %}

    {% if inventory_levels_to_zero.size > 0 %}
      {% for inventory_level in inventory_levels_to_zero %}
        {% action "shopify" %}
          mutation {
            inventoryAdjustQuantity(
              input: {
                inventoryLevelId: {{ inventory_level.id | json }}
                availableDelta: {{ inventory_level.available | times: -1 | json }}
              }
            ) {
              inventoryLevel {
                available
              }
              userErrors {
                field
                message
              }
            }
          }
        {% endaction %}
      {% endfor %}

      {% if options.restore_inventory_levels_the_next_day__boolean or options.restore_inventory_levels_on_demand__boolean %}
        {% comment %}
          -- NOTE: large inventories can run up against the 256kb Mechanic cache limit, so need to paginate (2000 entries ~200kb)
        {% endcomment %}
        {% assign inventory_levels_to_zero_in_groups = inventory_levels_to_zero | in_groups_of: 2000, fill_with: false %}

        {% assign base_cache_key = "inventory_to_restore:" | append: previous_midnight_s %}

        {% for inventory_levels_to_zero_group in inventory_levels_to_zero_in_groups %}
          {% assign cache_key = base_cache_key | append: "_group" | append: forloop.index0 %}
          {% assign cache_value = hash %}
          {% assign cache_value["value"] = inventory_levels_to_zero_group %}

          {% 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 %}
    {% endif %}
  {% endif %}
{% elsif event.topic contains "mechanic/" %}
  {% assign proceed = false %}

  {% if event.topic == "mechanic/scheduler/daily" and options.restore_inventory_levels_the_next_day__boolean %}
    {% assign proceed = true %}
  {% elsif event.topic == "mechanic/user/trigger" and options.restore_inventory_levels_on_demand__boolean %}
    {% assign proceed = true %}
  {% endif %}

  {% if proceed %}
    {% 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_levels_cache_group = cache[cache_key] | default: hash %}

    {% if inventory_levels_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_levels_cache_group = cache[cache_key] | default: hash %}
    {% endif %}

    {% assign inventory_levels_to_restore = array %}

    {% if event.preview %}
      {% assign inventory_levels_cache_group["value"] = array %}
      {% assign inventory_levels_cache_group["value"][0] = hash %}
      {% assign inventory_levels_cache_group["value"][0]["id"] = "gid://shopify/InventoryLevel/1234567890?inventory_item_id=1234567890" %}
      {% assign inventory_levels_cache_group["value"][0]["available"] = 20 %}
    {% endif %}

    {% if inventory_levels_cache_group != blank %}
      {% for n in (0..100) %}
        {% assign inventory_levels_to_restore = inventory_levels_to_restore | concat: inventory_levels_cache_group.value %}
        {% assign cache_keys = cache_keys | push: cache_key %}

        {% if inventory_levels_cache_group.next_key %}
          {% assign cache_key = inventory_levels_cache_group.next_key %}
          {% assign inventory_levels_cache_group = cache[cache_key] %}
        {% else %}
          {% break %}
        {% endif %}
      {% endfor %}
    {% endif %}

    {% log inventory_levels_to_restore_count: inventory_levels_to_restore.size, inventory_levels_to_restore: inventory_levels_to_restore, since: midnight %}

    {% for inventory_level in inventory_levels_to_restore %}
      {% action "shopify" %}
        mutation {
          inventoryAdjustQuantity(
            input: {
              inventoryLevelId: {{ inventory_level.id | json }}
              availableDelta: {{ inventory_level.available | json }}
            }
          ) {
            inventoryLevel {
              available
            }
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endfor %}

    {% if inventory_levels_to_restore != blank %}
      {% for cache_key in cache_keys %}
        {% action "cache", "del", cache_key %}
      {% endfor %}
    {% endif %}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Maximum daily orders
10
Only count orders matching this query
-status:cancelled
Restore inventory levels the next day
true