Keep SKUs up to date with barcodes
This task watches for new and updated products, copying variant barcodes over to the variants' inventory item SKUs. This occurs whenever a barcode is found, and the related SKU does not already have that value.
Try this taskWhen it runs
- Occurs whenever a product is created
- Occurs whenever a product is updated, ordered, or variants are added, removed or updated
- Occurs when a user manually triggers the task
- Occurs when a bulk operation is completed
Documentation
This task watches for new and updated products, copying variant barcodes over to the variants' inventory item SKUs. This occurs whenever a barcode is found, and the related SKU does not already have that value.
The task may also be run manually to scan all variants and inventory items in the shop, updating the unmatched SKUs as needed.
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/products/create
shopify/products/update
mechanic/user/trigger
mechanic/shopify/bulk_operation
How task subscriptions work
Task code
{% if event.topic == "shopify/products/create" or event.topic == "shopify/products/update" %}
{% comment %}
-- query all variants for this product, along with their inventory items
{% endcomment %}
{% capture query %}
query {
product(id: {{ product.admin_graphql_api_id | json }}) {
variants(first: 100) {
nodes {
id
barcode
sku
inventoryItem {
id
}
}
}
}
}
{% endcapture %}
{% assign result = query | shopify %}
{% assign variants = result.data.product.variants.nodes %}
{% elsif event.topic == "mechanic/user/trigger" %}
{% comment %}
-- use bulk op to query all variants in the shop, along with their inventory items
{% endcomment %}
{% capture bulk_operation_query %}
query {
productVariants {
edges {
node {
__typename
id
barcode
sku
inventoryItem {
id
}
}
}
}
}
{% endcapture %}
{% action "shopify" %}
mutation {
bulkOperationRunQuery(
query: {{ bulk_operation_query | json }}
) {
bulkOperation {
id
status
}
userErrors {
field
message
}
}
}
{% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
{% assign variants = bulkOperation.objects %}
{% endif %}
{% if event.preview %}
{% capture variants_json %}
[
{
"id": "gid://shopify/ProductVariant/1234567890",
"barcode": "ABC123",
"sku": "",
"inventoryItem": {
"id": "gid://shopify/InventoryItem/1234567890"
}
}
]
{% endcapture %}
{% assign variants = variants_json | parse_json %}
{% endif %}
{% comment %}
-- update skus on inventory items, as this is no longer supported on variants as of 2024-07 API
-- note: unlike variants, there is not yet a bulk update mutation for inventory items
{% endcomment %}
{% for variant in variants %}
{% if variant.barcode != blank and variant.sku != variant.barcode %}
{% action "shopify" %}
mutation {
inventoryItemUpdate(
id: {{ variant.inventoryItem.id | json }}
input: {
sku: {{ variant.barcode | json }}
}
) {
inventoryItem {
variant {
id
displayName
barcode
sku
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}
Mechanic Liquid reference