Hide out-of-stock products with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Hide out-of-stock products

by Isaac Bowen (team@usemechanic.com)

Whenever inventory for a product is updated, this task scans all variants and all fulfillment locations, adds up all inventory related to that product, and makes sure the product is hidden if the total inventory meets your "out of stock" threshold. Optionally, it'll send you an email when it does so.

Runs when a user triggers the task and when an inventory level is updated. Configuration includes email notification recipient, only monitor products with these tags, and out of stock inventory quantity.

15-day free trial – unlimited tasks

Documentation

Whenever inventory for a product is updated, this task scans all variants and all fulfillment locations, adds up all inventory related to that product, and makes sure the product is hidden if the total inventory meets your "out of stock" threshold.

To scan your entire catalog for out of stock products, use the "Run task" button. Otherwise, this task will run whenever an inventory level is updated.

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)
when an inventory level is updated (shopify/inventory_levels/update)
Options
email notification recipient (email), only monitor products with these tags (array), out of stock inventory quantity (number, required)
Script
{% if event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        ["product", 12345],
        {
          "published": false
        }
      ]
    }
  }

  {% if options.email_notification_recipient__email != blank %}
    {% capture email_subject %}
      Out of stock: Short Sleeve T-shirt
    {% endcapture %}

    {% capture email_body %}
      Hi there,

      Your product is out of stock!

      Visit https://{{ shop.domain }}/admin/products/12345 to manage this product.

      Thanks,
      - Mechanic, for {{ shop.name }}
    {% endcapture %}

    {
      "action": {
        "type": "email",
        "options": {
          "to": {{ options.email_notification_recipient__email | json }},
          "subject": {{ email_subject | unindent | strip | json }},
          "body": {{ email_body | unindent | strip | newline_to_br | json }}
        }
      }
    }
  {% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign unpublished_product_links = "" %}
  {% assign unpublished_product_count = 0 %}

  {% for product in shop.products.published %}
    {% assign product_qualifies = true %}
    {% assign product_tags = product.tags | split: ", " %}

    {% if options.only_monitor_products_with_these_tags__array != blank %}
      {% assign product_tags_qualify = false %}

      {% for tag in options.only_monitor_products_with_these_tags__array %}
        {% if product_tags contains tag %}
          {% assign product_tags_qualify = true %}
          {% break %}
        {% endif %}
      {% endfor %}

      {% if product_tags_qualify == false %}
        {% assign product_qualifies = false %}
      {% endif %}
    {% endif %}

    {% if product_qualifies %}
      {% assign aggregate_inventory_level = 0 %}

      {% for variant in product.variants %}
        {% for inventory_level in variant.inventory_levels %}
          {% assign aggregate_inventory_level = aggregate_inventory_level | plus: inventory_level.available %}
        {% endfor %}
      {% endfor %}

      {% if aggregate_inventory_level <= options.out_of_stock_inventory_quantity__number_required %}
        {
          "action": {
            "type": "shopify",
            "options": [
              "update",
              ["product", {{ product.id }}],
              {
                "published": false
              }
            ]
          }
        }

        {% capture unpublished_product_links %}{{ unpublished_product_links }}<li><a href="https://{{ shop.domain }}/admin/products/{{ product.id }}">{{ product.title }}</a> ({{ aggregate_inventory_level }})</li>{% endcapture %}
        {% assign unpublished_product_count = unpublished_product_count | plus: 1 %}
      {% endif %}
    {% endif %}
  {% endfor %}

  {% if unpublished_product_links != blank and options.email_notification_recipient__email != blank %}
    {% capture email_subject %}
      Found {{ unpublished_product_count }} {{ unpublished_product_count | pluralize: "product", "products" }} out of stock
    {% endcapture %}

    {% capture email_body %}
      Hi there,
      <br><br>
      These products were found to be under your out of stock minimum quantity ({{ options.out_of_stock_inventory_quantity__number_required }}), when adding up the inventory for each product. Each product has been unpublished, and is no longer available in your online storefront.
      <br>
      <ul>{{ unpublished_product_links }}</ul>
      <br>
      Thanks,
      <br>
      - Mechanic, for {{ shop.name }}
    {% endcapture %}

    {
      "action": {
        "type": "email",
        "options": {
          "to": {{ options.email_notification_recipient__email | json }},
          "subject": {{ email_subject | unindent | strip | json }},
          "body": {{ email_body | unindent | strip | json }}
        }
      }
    }
  {% endif %}
{% elsif event.topic contains "shopify/inventory_levels/" %}
  {% assign product = inventory_level.variant.product %}
  {% assign product_qualifies = true %}
  {% assign product_tags = product.tags | split: ", " %}

  {% if product.published_at == blank %}
    {% assign product_qualifies = false %}
  {% elsif options.only_monitor_products_with_these_tags__array != blank %}
    {% assign product_tags_qualify = false %}

    {% for tag in options.only_monitor_products_with_these_tags__array %}
      {% if product_tags contains tag %}
        {% assign product_tags_qualify = true %}
        {% break %}
      {% endif %}
    {% endfor %}

    {% if product_tags_qualify == false %}
      {% assign product_qualifies = false %}
    {% endif %}
  {% endif %}

  {% if product_qualifies %}
    {% assign aggregate_inventory_level = 0 %}

    {% for variant in product.variants %}
      {% for inventory_level in variant.inventory_levels %}
        {% assign aggregate_inventory_level = aggregate_inventory_level | plus: inventory_level.available %}
      {% endfor %}
    {% endfor %}

    {% if aggregate_inventory_level <= options.out_of_stock_inventory_quantity__number_required %}
      {
        "action": {
          "type": "shopify",
          "options": [
            "update",
            ["product", {{ product.id }}],
            {
              "published": false
            }
          ]
        }
      }

      {% if options.email_notification_recipient__email != blank %}
        {% capture email_subject %}
          Out of stock: {{ product.title }}
        {% endcapture %}

        {% capture email_body %}
          Hi there,

          Your product is out of stock! This product has been unpublished, and is no longer available in your online storefront.

          Visit https://{{ shop.domain }}/admin/products/{{ product.id }} to manage this product.

          Thanks,
          - Mechanic, for {{ shop.name }}
        {% endcapture %}

        {
          "action": {
            "type": "email",
            "options": {
              "to": {{ options.email_notification_recipient__email | json }},
              "subject": {{ email_subject | unindent | strip | json }},
              "body": {{ email_body | unindent | strip | newline_to_br | json }}
            }
          }
        }
      {% endif %}
    {% 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
Out of stock inventory quantity
0