Sync inventory across product variants, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Sync inventory across product variants

by Mechanic Team (team@usemechanic.com)

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 when a user triggers the task and every 10 minutes. Configuration includes product types to monitor.

15-day free trial – unlimited tasks

Documentation

Getting started

  1. Populate the list of product types that you'd like this task to monitor.
  2. In the Shopify admin, navigate to the Products > Inventory area. Use the tools here to ensure that all products, for the types you care about, have all their variant inventory in sync.
  3. Back in Mechanic, click the "Run task" button. Mechanic will scan your products, and cache the current inventory level for each one.
  4. Wait! :) Every ten minutes, Mechanic will check your inventory, and make any adjustments necessary to keep everything in sync. For example, if three different inventory items - within the same product - are each sold three different times, Mechanic will ensure that each of those items are lowered by a further 6, and that all others are lowered by 9.

Notes

  • This task only works with a single location. Multiple locations are not supported.
  • By default, Mechanic will check your inventory every 10 minutes. Feel free to change that subscription to "mechanic/scheduler/hourly", or something else that suits your needs.

Developer details

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.

Events
when a user triggers the task (mechanic/user/trigger)
every 10 minutes (mechanic/scheduler/10min)
Options
product types to monitor (array, required)
Script
{% assign search_query_parts = array %}
{% for product_type in options.product_types_to_monitor__array_required %}
  {% assign search_query_parts[search_query_parts.size] = "product_type:" | append: product_type %}
{% endfor %}
{% assign search_query = search_query_parts | join: " OR " %}

{% if event.preview %}
  {% capture variants_query %}
    {
      productVariants(
        first: 250
        query: {{ search_query | json }}
      ) {
        pageInfo {
          hasNextPage
        }
        edges {
          node {
            id
            inventoryQuantity
            inventoryItem {
              id
            }
            product {
              id
            }
          }
          cursor
        }
      }
    }
  {% endcapture %}

  {% assign variants_query_result = variants_query | shopify %}

  {% action "cache" %}
    {"set": {"key": "inventory_by_product:gid://shopify/Product/1234567890", "value": 500}}
  {% endaction %}

  {% capture primary_location_query %}
    {
      location {
        id
      }
    }
  {% endcapture %}

  {% assign primary_location_query_result = primary_location_query | shopify %}

  {% capture adjustment_query %}
    mutation {
      inventoryBulkAdjustQuantityAtLocation(
        inventoryItemAdjustments: []
        locationId: "gid://shopify/Location/123467890"
      ) {
        inventoryLevels {
          id
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endcapture %}

  {% action "shopify" adjustment_query %}
{% else %}
  {% assign variants_by_product = hash %}
  {% assign cursor = nil %}

  {% for n in (0..100) %}
    {% capture variant_query %}
      {
        productVariants(
          first: 250
          query: {{ search_query | json }}
          after: {{ cursor | json }}
        ) {
          pageInfo {
            hasNextPage
          }
          edges {
            node {
              id
              inventoryQuantity
              inventoryItem {
                id
              }
              product {
                id
              }
            }
            cursor
          }
        }
      }
    {% endcapture %}

    {% assign variant_query_result = variant_query | shopify %}

    {% assign variant_nodes = variant_query_result.data.productVariants.edges | map: "node" %}
    {% for variant_node in variant_nodes %}
      {% assign product_id = variant_node.product.id %}
      {% if variants_by_product[product_id] == nil %}
        {% assign variants_by_product[product_id] = array %}
      {% endif %}

      {% assign index = variants_by_product[product_id].size %}
      {% assign variants_by_product[product_id][index] = variant_node %}
    {% endfor %}

    {% if variant_query_result.data.productVariants.pageInfo.hasNextPage %}
      {% assign cursor = variant_query_result.data.productVariants.edges.last.cursor %}
      {% continue %}
    {% endif %}

    {% break %}
  {% endfor %}

  {% if event.topic == "mechanic/user/trigger" %}
    {% for pair in variants_by_product %}
      {% assign product_id = pair[0] %}
      {% assign variants = pair[1] %}

      {% assign unique_inventory_levels = variants | map: "inventoryQuantity" | uniq %}
      {% if unique_inventory_levels.size != 1 %}
        {"error": {{ "Ensure all products have even inventory levels across their variants, then try again. (Encountered mismatching inventory quantity in " | append: product_id | append: ")" | json }}}
        {% break %}
      {% else %}
        {% assign cache_key = "inventory_by_product:" | append: product_id %}
        {% action "cache" %}
          {"set": {"key": {{ cache_key | json }}, "value": {{ variants.first.inventoryQuantity | json }}}}
        {% endaction %}
      {% endif %}
    {% endfor %}
  {% elsif event.topic contains "mechanic/scheduler/" %}
    {% assign inventory_item_adjustments = array %}

    {% capture primary_location_query %}
      {
        location {
          id
        }
      }
    {% endcapture %}

    {% assign primary_location_query_result = primary_location_query | shopify %}

    {% assign primary_location_id = primary_location_query_result.data.location.id %}

    {% 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 starting_inventory = cache[cache_key] %}
      {% assign inventory_list = variants | map: "inventoryQuantity" %}
      {% assign inventory_total = inventory_list | sum %}
      {% assign inventory_diff = starting_inventory | times: inventory_list.size | minus: inventory_total %}
      {% assign new_inventory = starting_inventory | minus: inventory_diff %}

      {% if new_inventory == starting_inventory %}
        {"log": {{ product_id | append: ": No inventory adjustments needed." | json }}}
      {% else %}
        {% action "cache" %}
          {"set": {"key": {{ cache_key | json }}, "value": {{ new_inventory | json }}}}
        {% endaction %}

        {% for variant in variants %}
          {% assign adjustment = hash %}
          {% assign adjustment["inventoryItemId"] = variant.inventoryItem.id %}
          {% assign adjustment["availableDelta"] = new_inventory | minus: variant.inventoryQuantity %}
          {% assign inventory_item_adjustments[inventory_item_adjustments.size] = adjustment %}
        {% endfor %}
      {% endif %}
    {% endfor %}

    {% if inventory_item_adjustments.size > 0 %}
      {% capture adjustment_query %}
        mutation {
          inventoryBulkAdjustQuantityAtLocation(
            inventoryItemAdjustments: {{ inventory_item_adjustments | json | replace: '"inventoryItemId"', "inventoryItemId" | replace: '"availableDelta"', "availableDelta" }}
            locationId: {{ primary_location_id | json }}
          ) {
            inventoryLevels {
              id
            }
            userErrors {
              field
              message
            }
          }
        }
      {% endcapture %}

      {% action "shopify" adjustment_query %}
    {% endif %}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Product types to monitor
["Shirt"]