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.
Try this taskWhen it runs
Occurs whenever an inventory level is updated
What you choose
- Filter by these location names
- Filter by these product types
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!
Task code and subscriptions
You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.
Subscriptions
shopify/inventory_levels/update
How task subscriptions work
Option types and flags
- Filter by these location names (array)
- Filter by these product types (array)
Task code
{% assign filter_by_these_location_names = options.filter_by_these_location_names__array %}
{% assign filter_by_these_product_types = options.filter_by_these_product_types__array %}
{% assign changed_inventory_level_id = inventory_level.admin_graphql_api_id %}
{% comment %}
-- get location and product data from the inventory level that changed
{% endcomment %}
{% capture query %}
query {
inventoryLevel(id: {{ changed_inventory_level_id | json }}) {
location {
name
}
quantities(names: "available") {
quantity
}
item {
id
variants(first: 1) {
nodes {
product {
id
legacyResourceId
productType
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"inventoryLevel": {
"location": {
"name": {{ filter_by_these_location_names.first | json }}
},
"quantities": [
{
"quantity": 100
}
],
"item": {
"id": "gid://shopify/inventoryItem/1234567890",
"variants": {
"nodes": [
{
"product": {
"id": "gid://shopify/Product/1234567890",
"legacyResourceId": "1234567890",
"productType": {{ filter_by_these_product_types.first | json }}
}
}
]
}
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign new_quantity = result.data.inventoryLevel.quantities.first.quantity %}
{% assign location = result.data.inventoryLevel.location %}
{% assign product = result.data.inventoryLevel.item.vwariants.nodes.first.product %}
{% unless filter_by_these_location_names == blank or filter_by_these_location_names contains location.name %}
{% break %}
{% endunless %}
{% unless filter_by_these_product_types == blank or filter_by_these_product_types contains product.productType %}
{% break %}
{% endunless %}
{% assign cache_key = "product-inventory-level-" | append: product.legacyResourceId %}
{% comment %}
-- if there is a cache entry, then inventory level updates were made by this task in the last minute, so stop processing
{% endcomment %}
{% if cache[cache_key] != blank %}
{% log "This task adjusted this product's inventory levels within the last minute; skip further processing until cache expires." %}
{% break %}
{% endif %}
{% comment %}
-- get inventory levels of each variant at all locations to see which need to be adjusted
{% endcomment %}
{% assign inventory_adjustments = array %}
{% capture query %}
query {
product(id: {{ product.id | json }}) {
variants(first: 2048) {
nodes {
id
inventoryItem {
id
inventoryLevels(first: 25) {
nodes {
id
quantities(names: "available") {
quantity
}
location {
id
name
}
}
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"product": {
"variants": {
"nodes": [
{
"inventoryItem": {
"id": "gid://shopify/InventoryItem/2345678901",
"inventoryLevels": {
"nodes": [
{
"quantities": [
{
"quantity": 0
}
],
"location": {
"id": "gid://shopify/Location/1234567890",
"name": {{ filter_by_these_location_names.first | json }}
}
}
]
}
}
}
]
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% for variant in result.data.product.variants.nodes %}
{% for inventory_level in variant.inventoryItem.inventoryLevels.nodes %}
{% if inventory_level.id == changed_inventory_level_id %}
{% continue %}
{% endif %}
{% unless filter_by_these_location_names == blank or filter_by_these_location_names contains inventory_level.location.name %}
{% continue %}
{% endunless %}
{% if inventory_level.quantities.first.quantity != new_quantity %}
{% assign delta = new_quantity | minus: inventory_level.quantities.first.quantity %}
{% assign inventory_adjustment = hash %}
{% assign inventory_adjustment["inventoryItemId"] = variant.inventoryItem.id %}
{% assign inventory_adjustment["locationId"] = inventory_level.location.id %}
{% assign inventory_adjustment["delta"] = delta %}
{% assign inventory_adjustment["changeFromQuantity"] = inventory_level.quantities.first.quantity %}
{% assign inventory_adjustments = inventory_adjustments | push: inventory_adjustment %}
{% endif %}
{% endfor %}
{% endfor %}
{% comment %}
-- changeFromQuantity field is required as of 2026-04
https://shopify.dev/changelog/making-changefromquantity-field-required
-- Shopify will enforce idempotency for several mutations as of 2026-04
https://shopify.dev/changelog/making-idempotency-mandatory-for-inventory-adjustments-and-refund-mutations
{% endcomment %}
{% if inventory_adjustments != blank %}
{% action "cache", "setex", cache_key, 60, true %}
{% assign groups_of_inventory_adjustments = inventory_adjustments | in_groups_of: 250, fill_with: false %}
{% for group_of_inventory_adjustments in groups_of_inventory_adjustments %}
{% capture idempotent_key %}
{
"task_id": {{ task.id | json }},
"event_id": {{ event.id | json }},
"data": {{ group_of_inventory_adjustments | json }}
}
{% endcapture %}
{% action "shopify" %}
mutation {
inventoryAdjustQuantities(
input: {
reason: "correction"
name: "available"
changes: {{ group_of_inventory_adjustments | graphql_arguments }}
}
) @idempotent(key: {{ idempotent_key | json | md5 | json }}) {
inventoryAdjustmentGroup {
reason
changes(quantityNames: "available") {
name
delta
item {
id
sku
}
location {
name
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% endfor %}
{% endif %}
Mechanic Liquid reference
Default option values
- Filter by these location names
[]- Filter by these product types
[]