Reset inventory levels when they get too low, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Reset inventory levels when they get too low

by Isaac Bowen (team@usemechanic.com)

Use this task to automatically maintain minimum inventory levels. Optionally, filter by variant or product ID, or by variant option, or by product type, choosing whether to blacklist or whitelist your filter selections.

Runs when an inventory level is updated. Configuration includes minimum inventory level, location, whitelist filter mode, blacklist filter mode, filter for these variant ids, filter for these variant options, filter for these product types, and filter for these product ids.

15-day free trial – unlimited tasks

Documentation

This task runs as inventory levels are updated, auto-updating qualifying levels to meet the configured minimum.

This task can be configured with several resource IDs. Learn how to find these.

If a location ID is provided, this task will only adjust inventory for items at that location.

Additional filters may be configured, using either whitelist mode to only perform inventory adustments for items that match the filter, or blacklist mode to perform adjustments for all items that do not match the filter. All filters are optional, and each filter will be ignored if it is left unconfigured.

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.

Events
when an inventory level is updated (shopify/inventory_levels/update)
Options
minimum inventory level (number, required), location (number), whitelist filter mode (boolean), blacklist filter mode (boolean), filter for these variant ids (array, number), filter for these variant options (keyval), filter for these product types (array), filter for these product ids (array, number)
Script
{% comment %}
  An opinion on option order:

  {{ options.minimum_inventory_level__number_required }}
  {{ options.location_id__number }}
  {{ options.whitelist_filter_mode__boolean }}
  {{ options.blacklist_filter_mode__boolean }}
  {{ options.filter_for_these_variant_ids__array_number }}
  {{ options.filter_for_these_variant_options__keyval }}
  {{ options.filter_for_these_product_types__array }}
  {{ options.filter_for_these_product_ids__array_number }}
{% endcomment %}

{% if event.preview %}
  {% assign inventory_level = hash %}
  {% assign inventory_level["inventory_item_id"] = 1234567890 %}
  {% assign inventory_level["location_id"] = options.location_id__number | default: 1234567890 %}
  {% assign inventory_level["available"] = options.minimum_inventory_level__number_required | minus: 1 %}
  {% assign inventory_level["admin_graphql_api_id"] = "gid://shopify/InventoryLevel/1234567890?inventory_item_id=1234567890" %}
{% endif %}

{% assign minimum_inventory_level = options.minimum_inventory_level__number_required %}

{% assign location_id_qualifies = false %}
{% if options.location_id__number == blank or inventory_level.location_id == options.location_id__number %}
  {% assign location_id_qualifies = true %}
{% endif %}

{% assign inventory_level_requires_reset = false %}
{% if location_id_qualifies and inventory_level.available < minimum_inventory_level %}
  {% assign inventory_level_requires_reset = true %}
{% endif %}

{% if inventory_level_requires_reset %}
  {% capture query %}
    query {
      inventoryLevel(id: {{ inventory_level.admin_graphql_api_id | json }}) {
        item {
          variant {
            legacyResourceId
            selectedOptions {
              name
              value
            }
            product {
              productType
              legacyResourceId
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "inventoryLevel": {
            "item": {
              "variant": {
                "legacyResourceId": 
                  {% if options.whitelist_filter_mode__boolean %}
                    {{ options.filter_for_these_variant_ids__array_number.first | append: "" | json }}
                  {% else %}
                    {{ "now" | date: "%s" | json }}
                  {% endif %}
                  ,
                "selectedOptions": [
                  {% if options.whitelist_filter_mode__boolean %}
                    {
                      "name": {{ options.filter_for_these_variant_options__keyval.first[0] | json }},
                      "value": {{ options.filter_for_these_variant_options__keyval.first[1] | json }}
                    }
                  {% else %}
                    {
                      "name": {{ "now" | date: "%s" | sha256 | json }},
                      "value": {{ "now" | date: "%s" | sha256 | json }}
                    }
                  {% endif %}
                ],
                "product": {
                  "productType": 
                    {% if options.whitelist_filter_mode__boolean %}
                      {{ options.filter_for_these_product_types__array.first | json }}
                    {% else %}
                      {{ "now" | date: "%s" | sha256 | json }}
                    {% endif %}
                    ,
                  "legacyResourceId": 
                    {% if options.whitelist_filter_mode__boolean %}
                      {{ options.filter_for_these_product_ids__array_number.first | append: "" | json }}
                    {% else %}
                      {{ "now" | date: "%s" | json }}
                    {% endif %}
                }
              }
            }
          }
        }
      }
    {% endcapture %}

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

  {% assign inventory_level_filtered = nil %}

  {% assign variant_id = result.data.inventoryLevel.item.variant.legacyResourceId | times: 1 %}
  {% if options.filter_for_these_variant_ids__array_number contains variant_id %}
    {% assign inventory_level_filtered = "variant_id:" | append: variant_id %}
  {% endif %}

  {% for filter_option_keyval in options.filter_for_these_variant_options__keyval %}
    {% assign filter_option_name = filter_option_keyval[0] %}
    {% assign filter_option_value = filter_option_keyval[1] %}
    {% assign matching_filter_option = result.data.inventoryLevel.item.variant.selectedOptions | where: "name", filter_option_name | first %}

    {% if matching_filter_option and matching_filter_option.value == filter_option_value %}
      {% assign inventory_level_filtered = "variant_option:" | append: filter_option_name | append: "=" | append: filter_option_value %}
      {% break %}
    {% endif %}
  {% endfor %}

  {% assign product_id = result.data.inventoryLevel.item.variant.product.legacyResourceId | times: 1 %}
  {% if options.filter_for_these_product_ids__array_number contains product_id %}
    {% assign inventory_level_filtered = "product_id:" | append: product_id %}
  {% endif %}

  {% assign product_type = result.data.inventoryLevel.item.variant.product.productType %}
  {% if options.filter_for_these_product_types__array contains product_type %}
    {% assign inventory_level_filtered = "product_type:" | append: product_type %}
  {% endif %}

  {% if options.whitelist_filter_mode__boolean and options.blacklist_filter_mode__boolean %}
    {% error "Choose only one of 'Whitelist filter mode' or 'Blacklist filter mode'" %}
  {% elsif options.whitelist_filter_mode__boolean %}
    {% if options.filter_for_these_variant_ids__array_number == blank and options.filter_for_these_variant_options__keyval == blank and options.filter_for_these_product_types__array == blank and options.filter_for_these_product_ids__array_number == blank %}
      {% error "When using the whitelist filter mode, at least one filter must be configured." %}
    {% endif %}
  {% elsif options.whitelist_filter_mode__boolean == false and options.blacklist_filter_mode__boolean == false %}
    {% if options.filter_for_these_variant_ids__array_number != blank or options.filter_for_these_variant_options__keyval != blank or options.filter_for_these_product_types__array != blank or options.filter_for_these_product_ids__array_number != blank %}
      {% error "When using filters, a filter mode must be selected." %}
    {% endif %}
  {% endif }

  {% assign adjust_quantity = false %}
  {% if options.whitelist_filter_mode__boolean == false and options.blacklist_filter_mode__boolean == false %}
    {% log filter_mode: nil %}
    {% assign adjust_quantity = true %}
  {% elsif options.whitelist_filter_mode__boolean %}
    {% log filter_mode: "whitelist", filter_value: inventory_level_filtered %}

    {% if inventory_level_filtered != blank %}
      {% assign adjust_quantity = true %}
    {% endif %}
  {% elsif options.blacklist_filter_mode__boolean %}
    {% log filter_mode: "blacklist", filter_value: inventory_level_filtered %}

    {% if inventory_level_filtered == blank %}
      {% assign adjust_quantity = true %}
    {% endif %}
  {% endif %}

  {% if adjust_quantity %}
    {% action "shopify" %}
      mutation {
        inventoryAdjustQuantity(
          input: {
            inventoryLevelId: {{ inventory_level.admin_graphql_api_id | json }}
            availableDelta: {{ minimum_inventory_level | minus: inventory_level.available | json }}
          }
        ) {
          inventoryLevel {
            available
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Minimum inventory level
0