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 an inventory level is updated. Configuration includes email notification recipient and out of stock inventory quantity.

15-day free trial – unlimited tasks

Developer details

Events
when an inventory level is updated (shopify/inventory_levels/update)
Options
email notification recipient (email), 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 %}
{% else %}
  {% assign product = inventory_level.variant.product %}

  {% if product.published_at != blank %}
    {% 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 %}
          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 %}
Yep, that’s just Liquid! If this is interesting to you, check out our documentation! Mechanic is hugely developer-friendly. :)
Defaults
Out of stock inventory quantity
0