Reset inventory levels daily, with Mechanic.

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

Reset inventory levels daily

Use this task to bring all of your inventory levels to the same place, every night.

Runs Occurs every day at midnight (in local time). Configuration includes inventory level to apply and collection id.

15-day free trial – unlimited tasks

Documentation

Use this task to bring all of your inventory levels to the same place, every night.

This task resets your inventory levels every night, for a given collection ID.

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
mechanic/scheduler/daily
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
inventory level to apply (number, required), collection id (number, required)
Code
{% if event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "post",
        "/admin/inventory_levels/set.json",
        {
          "inventory_item_id": 1234567890,
          "location_id": {{ shop.primary_location_id | json }},
          "available": {{ options.inventory_level_to_apply__number_required | round | json }}
        }
      ]
    }
  }
{% else %}
  {% assign collection_id = options.collection_id__number_required | round %}
  {% assign collection = shop.collections[collection_id] %}

  {% unless collection %}
    {% capture message %}No collection found with ID #{{ collection_id | json }}.{% endcapture %}
    {"log": {{ message | json }}}
  {% endunless %}

  {% if collection.products.count == 0 %}
    {% capture message %}No products in collection #{{ collection_id | json }}.{% endcapture %}
    {"log": {{ message | json }}}
  {% endif %}

  {% for product in collection.products %}
    {% for variant in product.variants %}
      {% if variant.inventory_management != "shopify" %}
        {% capture message %}Inventory management policy is {{ variant.inventory_management | json }}, skipping inventory update for {{ variant.title }} on {{ product.title }}{% endcapture %}
        {"log": {{ message | json }}}
        {% continue %}
      {% endif %}

      {% for inventory_level in variant.inventory_levels %}
        {% if inventory_level.location_id != shop.primary_location_id %}
          {% continue %}
        {% endif %}

        {% if inventory_level.available == options.inventory_level_to_apply__number_required %}
          {% capture message %}Inventory level is already {{ inventory_level.available | json }}, skipping inventory update for {{ variant.title }} on {{ product.title }}{% endcapture %}
          {"log": {{ message | json }}}
          {% continue %}
        {% endif %}

        {
          "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": {{ options.inventory_level_to_apply__number_required | round | json }}
              }
            ]
          }
        }
      {% endfor %}
    {% endfor %}
  {% endfor %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more