Sync inventory levels to variant metafields with Mechanic.

Mechanic is an automation and development platform for Shopify. :)

Sync inventory levels to variant metafields

by Isaac Bowen (team@usemechanic.com)

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 when an inventory level is connected, when an inventory level is updated, and when a user triggers the task. Configuration includes metafield namespace and location ids and metafield keys.

15-day free trial – unlimited tasks

Documentation

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 the locations you've connfigured in this task.

Find your location IDs by navigating to Settings > Locations in the Shopify admin area. Then, select an individual location from the list. Its location ID can be found at the very end of the URL in your browser's address bar. For example, the location at this URL:

https://example.myshopify.com/admin/locations/1234567890

... has a location ID of 1234567890.

Use this ID on the left-hand side of the "Location IDs and metafield keys" option.

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 an inventory level is connected (shopify/inventory_levels/connect)
when an inventory level is updated (shopify/inventory_levels/update)
when a user triggers the task (mechanic/user/trigger)
Options
metafield namespace (required), location ids and metafield keys (keyval, required)
Script
{% assign metafield_namespace = options.metafield_namespace__required | strip %}

{% if event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "post",
        "/admin/products/1234567890/variants/1234567890/metafields.json",
        {
          "metafield": {
            "namespace": {{ metafield_namespace | json }},
            "key": {{ options.location_ids_and_metafield_keys__keyval_required.first.last | json }},
            "value_type": "integer",
            "value": 10
          }
        }
      ]
    }
  }
{% elsif event.topic contains "shopify/inventory_levels/" %}
  {% assign inventory_level_string = inventory_level.location_id | append: "" %}

  {% assign metafield_namespace = options.metafield_namespace__required | strip %}
  {% assign metafield_key = options.location_ids_and_metafield_keys__keyval_required[inventory_level_string] %}

  {% if metafield_key != blank %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "post",
          "/admin/products/{{ inventory_level.variant.product_id }}/variants/{{ inventory_level.variant.id }}/metafields.json",
          {
            "metafield": {
              "namespace": {{ metafield_namespace | json }},
              "key": {{ metafield_key | json }},
              "value_type": "integer",
              "value": {{ inventory_level.available | json }}
            }
          }
        ]
      }
    }
  {% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% for variant in shop.variants %}
    {% for inventory_level in variant.inventory_levels %}
      {% assign inventory_level_string = inventory_level.location_id | append: "" %}

      {% assign metafield_namespace = options.metafield_namespace__required | strip %}
      {% assign metafield_key = options.location_ids_and_metafield_keys__keyval_required[inventory_level_string] %}

      {% if metafield_key != blank and inventory_level.available != blank %}
        {
          "action": {
            "type": "shopify",
            "options": [
              "post",
              "/admin/products/{{ variant.product_id }}/variants/{{ variant.id }}/metafields.json",
              {
                "metafield": {
                  "namespace": {{ metafield_namespace | json }},
                  "key": {{ metafield_key | json }},
                  "value_type": "integer",
                  "value": {{ inventory_level.available | json }}
                }
              }
            ]
          }
        }
      {% endif %}
    {% endfor %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.