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 a minimum inventory level.

Runs when an inventory level is updated. Configuration includes minimum inventory level, location, variant ids to ignore, variant options to ignore, and product types to ignore.

15-day free trial – unlimited tasks

Documentation

To find your location ID, start by opening the locations settings in the Shopify admin. (Hint: Search the Shopify admin for "Locations".) Then, click on the location whose inventory levels you want to monitor. Your location ID is the series of numbers at the end of the location URL, in your browser's address bar. For example, if the URL is https://example.myshopify.com/admin/settings/locations/1234567890, the location ID is 1234568790.

To ignore certain variants by ID, add to the "Variant IDs to ignore" list.

To ignore many variants by a certain option pair (e.g. "Color" > "Heather Gray"), add to the "Variant options to ignore" list, with the option name on the left and the option value on the right. Any variant with an option matching an option name/value pair in this list will be ignored when monitoring inventory.

To ignore certain products by their product type, fill in the "Product types to ignore" option with a comma-separated list (e.g. "Shirts, Hats").

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), variant ids to ignore (array, number), variant options to ignore (keyval), product types to ignore (array)
Script
{% 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 %}
  {% assign variant = inventory_level.variant %}

  {% if options.variant_ids_to_ignore__array_number contains variant.id %}
    {% assign inventory_level_requires_reset = false %}
  {% endif %}
{% endif %}

{% if inventory_level_requires_reset %}
  {% comment %}
    We've already got `variant` by this point
  {% endcomment %}
  {% assign product = variant.product %}

  {% for blacklist_option_keyval in options.variant_options_to_ignore__keyval %}
    {% assign blacklist_option_name = blacklist_option_keyval[0] %}
    {% assign blacklist_option_value = blacklist_option_keyval[1] %}
    {% assign blacklist_variant_option_key = nil %}
    {"log":{"blacklist_option_name": {{ blacklist_option_name | json }}, "blacklist_option_value": {{ blacklist_option_value | json }}}}

    {% for product_option in product.options %}
      {% if product_option.name == blacklist_option_name %}
        {% assign blacklist_variant_option_key = "option" | append: forloop.index %}
        {% break %}
      {% endif %}
    {% endfor %}

    {"log":{"blacklist_variant_option_key": {{ blacklist_variant_option_key | json }}, "value": {{ variant[blacklist_variant_option_key] | json }}}}

    {% if blacklist_variant_option_key and variant[blacklist_variant_option_key] == blacklist_option_value %}
      {% assign inventory_level_requires_reset = false %}
      {% break %}
    {% endif %}
  {% endfor %}
{% endif %}

{% if inventory_level_requires_reset %}
  {% comment %}
    We've already got `product` by this point
  {% endcomment %}
  {% assign product_type = product.product_type %}

  {% if product_type != blank and options.product_types_to_ignore__array contains product_type %}
    {% assign inventory_level_requires_reset = false %}
  {% endif %}
{% endif %}

{% if inventory_level_requires_reset or event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "post",
        "/admin/inventory_levels/set.json",
        {
          "inventory_item_id": {{ inventory_level.inventory_item_id | json }},
          "location_id": {{ inventory_level.location_id | json }},
          "available": {{ minimum_inventory_level | json }}
        }
      ]
    }
  }
{% 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