Keep inventory levels in sync within products, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Keep inventory levels in sync within products

by Isaac Bowen (team@usemechanic.com)

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 when 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

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, 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
filter by these location names (array), filter by these product types (array)
Script
{% 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 %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.