Keep inventory levels in sync within products, with Mechanic.

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

Keep inventory levels in sync within products

Useful for selling, say, a limited print with different framing options, this task makes sure that a product's inventory levels, for each variant and location, are all kept in sync.

Runs Occurs whenever an inventory level is updated. Configuration includes filter by these location names and filter by these product types.

15-day free trial – unlimited tasks

Documentation

Useful for selling, say, a limited print with different framing options, this task makes sure that a product's inventory levels, for each variant and location, are all kept in sync.

When an inventory level is updated, this task will update the inventory for all other variants of the same product.

Optionally, configure this task to filter for certain location names and product types. If you leave these blank, the task will monitor and update inventory across all locations, and for all product types.

Limitations

Changes to multiple inventory items for a single product, within the span of 60 seconds, will result in only the first inventory change being applied to all inventory items. For example, if a customer purchases a framed and unframed version of the same print, the inventory levels for that product will only be decremented by 1, not by 2. If this is causing trouble for you, let us know!

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/inventory_levels/update
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
filter by these location names (array), filter by these product types (array)
Code
{% if event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "post",
        "/admin/inventory_levels/set.json",
        {
          "location_id": 123467890,
          "inventory_item_id": 123467890,
          "available": 30
        }
      ]
    }
  }

  {
    "action": {
      "type": "cache",
      "options": {
        "setex": {
          "key": "product-inventory-level-1234567890",
          "value": true,
          "ttl": 60
        }
      }
    }
  }
{% else %}
  {% if options.filter_by_these_location_names__array == blank or options.filter_by_these_location_names__array contains inventory_level.location.name %}
    {% if options.filter_by_these_product_types__array == blank or options.filter_by_these_product_types__array contains inventory_level.variant.product.product_type %}
      {% assign cache_key = "product-inventory-level-" | append: inventory_level.variant.product.id %}
      {% if cache[cache_key] == blank %}
        {% for other_variant in inventory_level.variant.product.variants %}
          {% for other_inventory_level in other_variant.inventory_levels %}
            {% if other_inventory_level.available != inventory_level.available %}
              {% if options.filter_by_these_location_names__array == blank or options.filter_by_these_location_names__array contains other_inventory_level.location.name %}
                {
                  "action": {
                    "type": "shopify",
                    "options": [
                      "post",
                      "/admin/inventory_levels/set.json",
                      {
                        "location_id": {{ other_inventory_level.location_id | json }},
                        "inventory_item_id": {{ other_inventory_level.inventory_item_id | json }},
                        "available": {{ inventory_level.available | json }}
                      }
                    ]
                  }
                }
              {% endif %}
            {% endif %}
          {% endfor %}
        {% endfor %}

        {
          "action": {
            "type": "cache",
            "options": {
              "setex": {
                "key": {{ cache_key | json }},
                "value": true,
                "ttl": 60
              }
            }
          }
        }
      {% endif %}
    {% endif %}
  {% endif %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more