Mechanic is a development and ecommerce automation platform for Shopify. :)
Useful for multiple price points, or for offering customizations of the same item, this task lets you offer multiple variant listings for what is ultimately the same stock. A purchase of a particular variant results in the inventory for all other variants, for the same product, being lowered by the amount ordered.
Runs Occurs when a user manually triggers the task and Occurs every 10 minutes. Configuration includes product types to monitor and only sync active products.
Useful for multiple price points, or for offering customizations of the same item, this task lets you offer multiple variant listings for what is ultimately the same stock. A purchase of a particular variant results in the inventory for all other variants, for the same product, being lowered by the amount ordered.
Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Gurus, 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.
{% assign product_types = options.product_types_to_monitor__array_required %} {% assign only_sync_active_products = options.only_sync_active_products__boolean %} {% assign primary_location = shop.locations[shop.primary_location_id] %} {% assign variants_by_product = hash %} {% assign cursor = nil %} {% assign search_query_parts = array %} {% for product_type in product_types %} {% assign search_query_parts[search_query_parts.size] = product_type | json | prepend: "product_type:" %} {% endfor %} {% capture search_query -%} location_id:{{ shop.primary_location_id }} ({{ search_query_parts | join: " OR " }}) managed:true {%- endcapture %} {% for n in (0..200) %} {% capture query %} query { productVariants( first: 150 query: {{ search_query | json }} after: {{ cursor | json }} ) { pageInfo { hasNextPage endCursor } nodes { id inventoryItem { id inventoryLevel(locationId: {{ primary_location.admin_graphql_api_id | json }}) { quantities(names: "available") { name quantity } } } product { id } } } } {% endcapture %} {% assign result = query | shopify %} {% if event.preview %} {% capture result_json %} { "data": { "productVariants": { "nodes": [ { "id": "gid://shopify/ProductVariant/1234567890", "inventoryItem": { "id": "gid://shopify/InventoryItem/1234567890", "inventoryLevel": { "quantities": [ { "name": "available", "quantity": 30 } ] } }, "product": { "id": "gid://shopify/Product/1234567890" } }, { "id": "gid://shopify/ProductVariant/2345678901", "inventoryItem": { "id": "gid://shopify/InventoryItem/2345678901", "inventoryLevel": { "quantities": [ { "name": "available", "quantity": {% if event.topic == "mechanic/user/trigger" %}30{% else %}31{% endif %} } ] } }, "product": { "id": "gid://shopify/Product/1234567890" } } ] } } } {% endcapture %} {% assign result = result_json | parse_json %} {% endif %} {% for variant in result.data.productVariants.nodes %} {% assign variants_by_product[variant.product.id] = variants_by_product[variant.product.id] | default: array | push: variant %} {% endfor %} {% if result.data.productVariants.pageInfo.hasNextPage %} {% assign cursor = result.data.productVariants.pageInfo.endCursor %} {% else %} {% break %} {% endif %} {% endfor %} {% if variants_by_product == blank %} {% log message: "No matching product variants found! Check your product type settings for this task.", search_query: search_query %} {% break %} {% endif %} {% if event.topic == "mechanic/user/trigger" %} {% for pair in variants_by_product %} {% assign product_id = pair[0] %} {% assign variants = pair[1] %} {% assign available_quantites = array %} {% for variant in variants %} {% assign available_quantites = available_quantites | push: variant.inventoryItem.inventoryLevel.quantities.first.quantity %} {% endfor %} {% assign inventory_total = available_quantites | sum %} {% assign expected_inventory_total = available_quantites.first | times: available_quantites.size %} {% if inventory_total != expected_inventory_total %} {% error message: "Product variant inventory levels are not in sync. Manually ensure everything is at the same level, then try again.", product_id: product_id, expected_inventory_total: expected_inventory_total, inventory_total: inventory_total, available_quantites: available_quantites %} {% else %} {% assign cache_key = "inventory_by_product:" | append: product_id %} {% action "cache", "set", cache_key, available_quantites.first %} {% endif %} {% endfor %} {% elsif event.topic contains "mechanic/scheduler/" %} {% assign inventory_adjustments = array %} {% for pair in variants_by_product %} {% assign product_id = pair[0] %} {% assign variants = pair[1] %} {% assign cache_key = "inventory_by_product:" | append: product_id %} {% assign inventory_old = cache[cache_key] %} {% if event.preview %} {% assign inventory_old = 30 %} {% endif %} {% if inventory_old == blank %} {% error message: "The inventory for this product has not yet been cached. Manually synchronize the inventory for this product and run this task manually per steps 2 and 3 in the task instructions.", product_id: product_id %} {% endif %} {% assign available_quantites = array %} {% for variant in variants %} {% assign available_quantites = available_quantites | push: variant.inventoryItem.inventoryLevel.quantities.first.quantity %} {% endfor %} {% assign inventory_total_old = inventory_old | times: available_quantites.size %} {% assign inventory_total_new = available_quantites | sum %} {% assign inventory_total_diff = inventory_total_new | minus: inventory_total_old %} {% assign inventory_new = inventory_old | plus: inventory_total_diff %} {% log inventory_old: inventory_old, inventory_total_old: inventory_total_old, inventory_total_new: inventory_total_new, inventory_total_diff: inventory_total_diff, inventory_new: inventory_new, product_id: product_id, variants_count: variants.size, variants: variants %} {% if inventory_new == inventory_old %} {% log "No inventory adjustments needed for this product." %} {% continue %} {% endif %} {% action "cache", "set", cache_key, inventory_new %} {% for variant in variants %} {% assign inventory_adjustment = hash %} {% assign inventory_adjustment["inventoryItemId"] = variant.inventoryItem.id %} {% assign inventory_adjustment["locationId"] = primary_location.admin_graphql_api_id %} {% assign inventory_adjustment["delta"] = inventory_new | minus: variant.inventoryItem.inventoryLevel.quantities.first.quantity %} {% assign inventory_adjustments = inventory_adjustments | push: inventory_adjustment %} {% endfor %} {% endfor %} {% if inventory_adjustments != blank %} {% assign groups_of_inventory_adjustments = inventory_adjustments | in_groups_of: 250, fill_with: false %} {% for group_of_inventory_adjustments in groups_of_inventory_adjustments %} {% action "shopify" %} mutation { inventoryAdjustQuantities( input: { reason: "correction" name: "available" changes: {{ group_of_inventory_adjustments | graphql_arguments }} } ) { inventoryAdjustmentGroup { reason changes { name delta quantityAfterChange item { id sku } location { name } } } userErrors { code field message } } } {% endaction %} {% endfor %} {% endif %} {% endif %}
["Shirt"]