Keep variant inventory in sync across locations
When an inventory level is updated, this task will adjust the "available" inventory for all other locations of the same variant to be the same quantity. Optionally, configure this task to filter for specific product types. Leaving this option blank means the task will monitor and update inventory for all products in the shop.
Try this taskWhen it runs
Occurs whenever an inventory level is updated
What you choose
Filter by these product types
Documentation
When an inventory level is updated, this task will adjust the "available" inventory for all other locations of the same variant to be the same quantity. Optionally, configure this task to filter for specific product types. Leaving this option blank means the task will monitor and update inventory for all products in the shop.
CAUTION: This task might not make accurate adjustments in certain high-volume scenarios. For example, an inventory import that sets mismatched inventory levels for a variant monitored by this task will cause a cascading effect of reapplying the deltas between each incorrect inventory level.
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 product types (array)
Task code
{% assign filter_by_these_product_types = options.filter_by_these_product_types__array %}
{% comment %}
-- query for "available" quantities at all locations where this inventory item / variant is stocked
{% endcomment %}
{% capture query %}
query {
inventoryLevel(id: {{ inventory_level.admin_graphql_api_id | json }}) {
item {
id
variants(first: 1) {
nodes {
legacyResourceId
product {
productType
}
}
}
inventoryLevels(first: 100) {
nodes {
id
location {
id
}
quantities(names: "available") {
quantity
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"inventoryLevel": {
"item": {
"id": "gid://shopify/InventoryItem/1234567890",
"variants": {
"nodes": [
{
"legacyResourceId": "1234567890",
"product": {
"productType": {{ filter_by_these_product_types.first | json }}
}
}
]
},
"inventoryLevels": {
"nodes": [
{
"id": "gid://shopify/InventoryLevel/1234567890?inventory_item_id=1234567890",
"location": {
"id": "gid://shopify/Location/1234567890"
},
"quantities": [
{
"quantity": 1
}
]
},
{
"id": "gid://shopify/InventoryLevel/2345678901?inventory_item_id=1234567890",
"location": {
"id": "gid://shopify/Location/2345678901"
},
"quantities": [
{
"quantity": 0
}
]
}
]
}
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign inventory_item = result.data.inventoryLevel.item %}
{% assign variant = inventory_item.variants.nodes.first %}
{% assign product = variant.product %}
{% assign inventory_levels = inventory_item.inventoryLevels.nodes %}
{% comment %}
-- if product types are configured in the task, make sure this inventory level update belongs to a product with one of the configured types
{% endcomment %}
{% if filter_by_these_product_types != blank %}
{% unless filter_by_these_product_types contains product.productType %}
{% log
message: "Product type is not in the configured product types; skipping.",
variant: variant,
filter_by_these_product_types: filter_by_these_product_types
%}
{% break %}
{% endunless %}
{% endif %}
{% if inventory_levels.size < 2 %}
{% log
message: "This variant is only stocked at a single location; no inventory sync needed.",
variant: variant
%}
{% break %}
{% endif %}
{% comment %}
-- get the "available" quantity for the inventory level that caused the update event
{% endcomment %}
{% assign source_inventory_level
= inventory_levels
| where: "id", inventory_level.admin_graphql_api_id
| first
%}
{% assign source_quantity = source_inventory_level.quantities.first.quantity %}
{% comment %}
-- since this task updates ALL inventory levels of a variant in bulk, then a source quantity that doesn't equal the cached value for this variant indicates the change was likely not made via a prior run of this task
{% endcomment %}
{% assign cache_key = "variant-inventory-level-" | append: variant.legacyResourceId %}
{% assign cached_value = cache[cache_key] %}
{% if cached_value != blank %}
{% if cached_value == source_quantity %}
{% log
message: "The 'available' inventory level of this variant at this location (i.e. source quantity) matches the cached value for this variant; no updates to make.",
cached_value: cached_value,
source_quantity: source_quantity
%}
{% break %}
{% endif %}
{% log
message: "The 'available' inventory level of this variant at this location (i.e. source quantity) does not match the cached value for this variant; update the cache and adjust the other levels as needed.",
cached_value: cached_value,
source_quantity: source_quantity
%}
{% endif %}
{% comment %}
-- no cached value exists OR this new value does not match it, so set it and then adjust "available" quantities across locations, using the source quantity to determine deltas
{% endcomment %}
{% action "cache", "setex", cache_key, 60, source_quantity %}
{% assign adjustments = array %}
{% for inventory_level in inventory_levels %}
{% assign delta = source_quantity | minus: inventory_level.quantities.first.quantity %}
{% if delta != 0 %}
{% assign adjustment = hash %}
{% assign adjustment["inventoryItemId"] = inventory_item.id %}
{% assign adjustment["locationId"] = inventory_level.location.id %}
{% assign adjustment["delta"] = delta %}
{% assign adjustment["changeFromQuantity"] = inventory_level.quantities.first.quantity %}
{% assign adjustments = adjustments | push: adjustment %}
{% endif %}
{% 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 adjustments != blank %}
{% capture idempotent_key %}
{
"task_id": {{ task.id | json }},
"event_id": {{ event.id | json }},
"data": {{ adjustments | json }}
}
{% endcapture %}
{% action "shopify" %}
mutation {
inventoryAdjustQuantities(
input: {
name: "available"
reason: "correction"
changes: {{ 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 %}
{% endif %}
Mechanic Liquid reference