Mechanic is a development and ecommerce automation platform for Shopify. :)
Use this task to easily make your inventory levels available in your online storefront theme. This task monitors for changes to inventory, and copies inventory availability figures to metafields on the related variant.
Runs Occurs whenever an inventory level is connected, Occurs whenever an inventory level is updated, Occurs when a user manually triggers the task, and Occurs when a bulk operation is completed. Configuration includes metafield namespace and location ids and metafield keys.
Use this task to easily make your inventory levels available in your online storefront theme. This task monitors for changes to inventory, and copies inventory availability figures to metafields on the related variant.
This task runs automatically, whenever an inventory level is adjusted or whenever a variant is stocked for the first time at a new location.
Run this task manually to backfill metafields for all variants that are stocked in the locations you've configured in this task.
"Location IDs and metafield keys" is a keyval option, where the left-hand side (the key) should contain your location IDs, and the right-hand side (the value) should contain the "metafield key" that you want to associate with each location (e.g. "1234567890" <> "shop", "2345678901" <> "warehouse").
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?)
shopify/inventory_levels/connect shopify/inventory_levels/update mechanic/user/trigger mechanic/shopify/bulk_operation
{% assign metafield_namespace = options.metafield_namespace__required | strip %}
{% assign location_ids_and_metafield_keys = options.location_ids_and_metafield_keys__keyval_required %}
{% if event.topic contains "shopify/inventory_levels/" %}
{% if event.preview %}
{% capture inventory_level_json %}
{
"location_id": {{ location_ids_and_metafield_keys.first.first }},
"admin_graphql_api_id": "gid://shopify/InventoryLevel/1234567890?inventory_item_id=1234567890"
}
{% endcapture %}
{% assign inventory_level = inventory_level_json | parse_json %}
{% endif %}
{% assign inventory_level_string = inventory_level.location_id | append: "" %}
{% assign metafield_key = location_ids_and_metafield_keys[inventory_level_string] %}
{% if metafield_key == blank %}
{% log "Inventory level change did not occur at a location configured in this task." %}
{% break %}
{% endif %}
{% capture query %}
query {
inventoryLevel(id: {{ inventory_level.admin_graphql_api_id | json }}) {
id
quantities(names: "available") {
name
quantity
}
item {
variants(first: 1) {
nodes {
id
metafield(
namespace: {{ metafield_namespace | json }}
key: {{ metafield_key | json }}
) {
value
}
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"inventoryLevel": {
"quantities": [
{
"name": "available",
"quantity": 20
}
],
"item": {
"variants": {
"nodes": [
{
"id": "gid://shopify/ProductVariant/1234567890"
}
]
}
}
}
}
}
{% endcapture %}
{% assign result = result_json | parse_json %}
{% endif %}
{% assign available_inventory = result.data.inventoryLevel.quantities.first.quantity | append: "" %}
{% assign variant = result.data.inventoryLevel.item.variants.nodes.first %}
{% assign metafield_value = variant.metafield.value %}
{% if available_inventory == metafield_value %}
{% log "Metafield value already matches the available inventory level." %}
{% break %}
{% endif %}
{% action "shopify" %}
mutation {
metafieldsSet(
metafields: [
{
ownerId: {{ variant.id | json }}
namespace: {{ metafield_namespace | json }}
key: {{ metafield_key | json }}
type: "number_integer"
value: {{ available_inventory | json }}
}
]
) {
metafields {
id
namespace
key
type
value
owner {
... on ProductVariant {
id
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% capture bulk_operation_query %}
query {
productVariants {
edges {
node {
id
inventoryItem {
id
inventoryLevels {
edges {
node {
__typename
id
quantities(names: "available") {
name
quantity
}
location {
legacyResourceId
}
}
}
}
}
}
}
}
}
{% endcapture %}
{% action "shopify" %}
mutation {
bulkOperationRunQuery(
query: {{ bulk_operation_query | json }}
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}
{% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
{% if event.preview %}
{% capture jsonl_string %}
{"__typename":"InventoryLevel","quantities":[{"name":"available","quantity":20}],"location":{"legacyResourceId":{{ location_ids_and_metafield_keys.first.first | json }}},"__parentId":"gid://shopify/ProductVariant/1234567890"}
{% endcapture %}
{% assign bulkOperation = hash %}
{% assign bulkOperation["objects"] = jsonl_string | parse_jsonl %}
{% endif %}
{% assign inventory_levels = bulkOperation.objects | where: "__typename", "InventoryLevel" %}
{% assign metafield_set_inputs = array %}
{% for inventory_level in inventory_levels %}
{% assign metafield_key = location_ids_and_metafield_keys[inventory_level.location.legacyResourceId] %}
{% if metafield_key != blank and inventory_level.quantities.first.quantity != blank %}
{% assign metafield_set_input = hash %}
{% assign metafield_set_input["ownerId"] = inventory_level.__parentId %}
{% assign metafield_set_input["namespace"] = metafield_namespace %}
{% assign metafield_set_input["key"] = metafield_key %}
{% assign metafield_set_input["type"] = "number_integer" %}
{% assign metafield_set_input["value"] = inventory_level.quantities.first.quantity | append: "" %}
{% assign metafield_set_inputs = metafield_set_inputs | push: metafield_set_input %}
{% endif %}
{% endfor %}
{% if metafield_set_inputs != blank %}
{% assign groups_of_metafield_set_inputs = metafield_set_inputs | in_groups_of: 25, fill_with: false %}
{% for group_of_metafield_set_inputs in groups_of_metafield_set_inputs %}
{% action "shopify" %}
mutation {
metafieldsSet(
metafields: {{ group_of_metafield_set_inputs | graphql_arguments }}
) {
metafields {
id
namespace
key
type
value
owner {
... on ProductVariant {
id
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% endfor %}
{% endif %}
{% endif %}