Update product description when out of stock, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

Update product description when out of stock

This task prepends an HTML message to the beginning of the product description, for each product that goes out of stock, and then restores the original product description when the product is back in stock. Optionally, you may choose to append the HTML message to the end of the product description, instead of prepending it to the beginning.

Runs Occurs whenever an inventory level is updated and Occurs when a user manually triggers the task. Configuration includes only modify products with this tag, add this html to product description, and add the html to end of product description instead of beginning.

15-day free trial – unlimited tasks

Documentation

This task prepends an HTML message to the beginning of the product description, for each product that goes out of stock, and then restores the original product description when the product is back in stock. Optionally, you may choose to append the HTML message to the end of the product description, instead of prepending it to the beginning.

The task runs when inventory levels change, or may be run manually on demand. Manual runs will ignore products that do not have inventory tracking enabled.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, 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.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
shopify/inventory_levels/update
mechanic/user/trigger
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
only modify products with this tag , add this html to product description (multiline, required) , add the html to end of product description instead of beginning (boolean)
Code
{% assign only_modify_products_with_this_tag = options.only_modify_products_with_this_tag %}
{% assign add_this_html_to_product_description = options.add_this_html_to_product_description__multiline_required %}
{% assign add_the_html_to_end_of_product_description = options.add_the_html_to_end_of_product_description_instead_of_beginning__boolean %}

{% assign products_to_check = array %}

{% if event.topic == "shopify/inventory_levels/update" %}
  {% comment %}
    -- get product and variant data
    -- sum inventoryQuantity from all variants instead of using product.totalInventory due to calculated field lag
  {% endcomment %}

  {% capture query %}
    query {
      inventoryLevel(
        id: {{ inventory_level.admin_graphql_api_id | json }}
      ) {
        item {
          variants(first: 1) {
            nodes {
              product {
                id
                descriptionHtml
                tags
                totalInventory
                variants(first: 250) {
                  nodes {
                    inventoryQuantity
                  }
                }
              }
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "inventoryLevel": {
            "item": {
              "variants": {
                "nodes": [
                  {
                    "product": {
                      "id": "gid://shopify/Product/1234567890",
                      "descriptionHtml": "<p>The best widget on the market!</p>",
                      "tags": {{ only_modify_products_with_this_tag | json }},
                      "variants": {
                        "nodes": [
                          {
                            "inventoryQuantity": 0
                          }
                        ]
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    {% endcapture %}

    {% assign result = result_json | parse_json %}
  {% endif %}

  {% assign product = result.data.inventoryLevel.item.variants.nodes.first.product %}
  {% assign available_inventory = product.variants.nodes | map: "inventoryQuantity" | sum %}

  {% unless event.preview %}
    {% log
      available_inventory_summed_from_variants: available_inventory,
      product_total_inventory_laggy_calc: product.totalInventory
    %}
  {% endunless %}

  {% if only_modify_products_with_this_tag != blank %}
    {% if product.tags contains only_modify_products_with_this_tag %}
      {% assign products_to_check[0] = product %}

    {% else %}
      {% log
        message: "This product does not have the tag being filtered by; skipping",
        tag_required: only_modify_products_with_this_tag,
        product_tags: product.tags
      %}
    {% endif %}

  {% else %}
    {% assign products_to_check[0] = product %}
  {% endif %}

{% elsif event.topic == "mechanic/user/trigger" %}
  {% assign cursor = nil %}
  {% assign products_query = nil %}

  {% if only_modify_products_with_this_tag != blank %}
    {% assign products_query = only_modify_products_with_this_tag | json | prepend: "tag:" %}
  {% endif %}

  {% for n in (1..100) %}
    {% capture query %}
      query {
        products(
          first: 250
          after: {{ cursor | json }}
          query: {{ products_query | json }}
        ) {
          pageInfo {
            hasNextPage
            endCursor
          }
          nodes {
            id
            descriptionHtml
            tracksInventory
            totalInventory
          }
        }
      }
    {% endcapture %}

    {% assign result = query | shopify %}

    {% if event.preview %}
      {% capture result_json %}
        {
          "data": {
            "products": {
              "nodes": [
                {
                  "id": "gid://shopify/Product/1234567890",
                  "descriptionHtml": "<p>The best widget on the market!</p>",
                  "tracksInventory": true,
                  "totalInventory": 0
                }
              ]
            }
          }
        }
      {% endcapture %}

      {% assign result = result_json | parse_json %}
    {% endif %}

    {% assign products = result.data.products.nodes | where: "tracksInventory" %}
    {% assign products_to_check = products_to_check | concat: products %}

    {% if result.data.products.pageInfo.hasNextPage %}
      {% assign cursor = result.data.products.pageInfo.endCursor %}
    {% else %}
      {% break %}
    {% endif %}
  {% endfor %}
{% endif %}

{% for product in products_to_check %}
  {% assign cache_key = product.id | append: "__description_to_restore" %}
  {% assign cached_description_html = cache[cache_key] %}

  {% if event.topic == "mechanic/user/trigger" %}
    {% comment %}
      -- note: ok to use product.totalInventory on manual runs since the calculated field lag would be negligible
    {% endcomment %}

    {% assign available_inventory = product.totalInventory %}
  {% endif %}

  {% if available_inventory > 0 %}
    {% if cached_description_html != blank %}
      {% action "shopify" %}
        mutation {
          productUpdate(
            product: {
              id: {{ product.id | json }}
              descriptionHtml: {{ cached_description_html | json }}
            }
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}

      {% action "cache", "del", cache_key %}
    {% endif %}

  {% else %}
    {% unless product.descriptionHtml contains add_this_html_to_product_description %}
      {% if add_the_html_to_end_of_product_description %}
        {% assign updated_description_html = product.descriptionHtml | append: add_this_html_to_product_description %}
      {% else %}
        {% assign updated_description_html = product.descriptionHtml | prepend: add_this_html_to_product_description %}
      {% endif %}

      {% action "shopify" %}
        mutation {
          productUpdate(
            product: {
              id: {{ product.id | json }}
              descriptionHtml: {{ updated_description_html | json }}
            }
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}

      {% action "cache", "set", cache_key, product.descriptionHtml %}
    {% endunless %}
  {% endif %}
{% endfor %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more