Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

  • Occurs whenever an inventory level is updated
  • Occurs when a user manually triggers the task

What you choose

  • Only modify products with this tag
  • Add this HTML to product description
  • Add the HTML to end of product description instead of beginning

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.

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

shopify/inventory_levels/update
mechanic/user/trigger
How task subscriptions work

Option types and flags

Task 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 %}
Mechanic Liquid reference

Default option values

Add the HTML to end of product description instead of beginning
false