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 IL {
inventoryLevel(id: {{ changed_inventory_level_id | json }}) {
location {
name
}
quantities(names: "available") {
quantity
}
item {
id
variant {
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",
"variant": {
"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.variant.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 %}
{% break %}
{% endif %}
{% comment %}
-- get inventory levels of each variant at all locations to see which need to be updated
-- support up to 2K variants
{% endcomment %}
{% assign cursor = nil %}
{% assign inventory_inputs = array %}
{% for n in (1..8) %}
{% capture query %}
query {
product(id: {{ product.id | json }}) {
variants(
first: 250
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
inventoryItem {
id
inventoryLevels(first: 250) {
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 inventory_input = hash %}
{% assign inventory_input["inventoryItemId"] = variant.inventoryItem.id %}
{% assign inventory_input["locationId"] = inventory_level.location.id %}
{% assign inventory_input["quantity"] = new_quantity %}
{% assign inventory_inputs = inventory_inputs | push: inventory_input %}
{% endif %}
{% endfor %}
{% endfor %}
{% if result.data.product.variants.pageInfo.hasNextPage %}
{% assign cursor = result.data.product.variants.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% if inventory_inputs != blank %}
{% comment %}
-- set a cache entry for 60 seconds
{% endcomment %}
{% action "cache", "setex", cache_key, 60, true %}
{% comment %}
-- set the inventory levels in batches of 250
{% endcomment %}
{% assign groups_of_inventory_inputs = inventory_inputs | in_groups_of: 250, fill_with: false %}
{% for group_of_inventory_inputs in groups_of_inventory_inputs %}
{% action "shopify" %}
mutation {
inventorySetQuantities(
input: {
name: "available"
reason: "correction"
ignoreCompareQuantity: true
quantities: {{ group_of_inventory_inputs | graphql_arguments }}
}
) {
userErrors {
code
field
message
}
}
}
{% endaction %}
{% endfor %}
{% endif %}
Mechanic Liquid reference
Default option values
- Filter by these location names
[]- Filter by these product types
[]