Auto-update inventory policy based on a "preorder" tag, with Mechanic.

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

Auto-update inventory policy based on a "preorder" tag

This task manages your published product catalog, checking for the presence of a "preorder" tag, or a tag that starts with "preorder_" (like "preorder_summer"). If this tag is found, the task updates the product to allow customers to purchase the product after it's out of stock. If no preorder tag is found, the task makes sure customers cannot purchase the product if it's out of stock.

Runs Occurs every day at midnight (in local time) and Occurs when a user manually triggers the task.

15-day free trial – unlimited tasks

Documentation

This task manages your published product catalog, checking for the presence of a "preorder" tag, or a tag that starts with "preorder_" (like "preorder_summer"). If this tag is found, the task updates the product to allow customers to purchase the product after it's out of stock. If no preorder tag is found, the task makes sure customers cannot purchase the product if it's out of stock.

This task scans your product catalog nightly or when you hit the "Run task" button, checking for the presence of a "preorder" tag, or a tag that starts with "preorder_" (like "preorder_summer"). If this tag is found, the task updates the product to allow customers to purchase the product after it's out of stock. If no preorder tag is found, the task makes sure customers cannot purchase the product if it's out of stock.

Notes:

  • For efficiency, this task only scans published products in your catalog.
  • This task supports four event topics:

    • Use mechanic/scheduler/daily for a daily scan, at midnight.
    • Use mechanic/user/trigger to run the task on-demand, with the push of a button.
    • Use shopify/products/create to run the task for each product as it's created.
    • Use shopify/products/update to run the task for each product as it's updated.

    Out of the box, the task comes configured with mechanic/scheduler/daily and mechanic/user/trigger. Use the "Subscriptions" setting (under "Show Advanced", in the task editor) to swap out subscriptions as needed.

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
mechanic/scheduler/daily
mechanic/user/trigger
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Code
{% if event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "update",
        [
          "variant",
          12345
        ],
        {
          "inventory_policy": "continue"
        }
      ]
    }
  }
{% elsif event.topic == "shopify/products/create" or event.topic == "shopify/products/update" %}
  {% if product.published_at == blank %}
    {"log": "Product is not published; skipping scan"}
  {% else %}
    {% assign updated_variants_count = 0 %}
    {% assign product_updated = false %}

    {% assign product_tags = product.tags | split: ", " %}
    {% assign allow_preorders = false %}
    {% for tag in product_tags %}
      {% assign tag_parts = tag | split: "_" %}
      {% if tag_parts[0] == "preorder" %}
        {% assign allow_preorders = true %}
        {% break %}
      {% endif %}
    {% endfor %}

    {% assign inventory_policy_update = nil %}

    {% for variant in product.variants %}
      {% if allow_preorders and variant.inventory_policy == "deny" %}
        {% assign inventory_policy_update = "continue" %}
      {% elsif allow_preorders == false and variant.inventory_policy == "continue" %}
        {% assign inventory_policy_update = "deny" %}
      {% endif %}

      {% if inventory_policy_update %}
        {% assign product_updated = true %}
        {% assign updated_variants_count = updated_variants_count | plus: 1 %}
        {
          "action": {
            "type": "shopify",
            "options": [
              "update",
              [
                "variant",
                {{ variant.id | json }}
              ],
              {
                "inventory_policy": {{ inventory_policy_update | json }}
              }
            ]
          }
        }
      {% endif %}
    {% endfor %}

    {% capture log_message %}
      {{ product.variants.size }} variants scanned, {{ updated_variants_count }} variants required an update
    {% endcapture %}

    {"log": {{ log_message | strip | json }}}
  {% endif %}
{% else %}
  {% assign products_count = 0 %}
  {% assign updated_products_count = 0 %}
  {% assign updated_variants_count = 0 %}

  {% for product in shop.products.published %}
    {% assign product_updated = false %}

    {% assign product_tags = product.tags | split: ", " %}
    {% assign allow_preorders = false %}
    {% for tag in product_tags %}
      {% assign tag_parts = tag | split: "_" %}
      {% if tag_parts[0] == "preorder" %}
        {% assign allow_preorders = true %}
        {% break %}
      {% endif %}
    {% endfor %}

    {% assign inventory_policy_update = nil %}

    {% for variant in product.variants %}
      {% if allow_preorders and variant.inventory_policy == "deny" %}
        {% assign inventory_policy_update = "continue" %}
      {% elsif allow_preorders == false and variant.inventory_policy == "continue" %}
        {% assign inventory_policy_update = "deny" %}
      {% endif %}

      {% if inventory_policy_update %}
        {% assign product_updated = true %}
        {% assign updated_variants_count = updated_variants_count | plus: 1 %}
        {
          "action": {
            "type": "shopify",
            "options": [
              "update",
              [
                "variant",
                {{ variant.id | json }}
              ],
              {
                "inventory_policy": {{ inventory_policy_update | json }}
              }
            ]
          }
        }
      {% endif %}
    {% endfor %}

    {% assign products_count = products_count | plus: 1 %}
    {% if product_updated %}
      {% assign updated_products_count = updated_products_count | plus: 1 %}
    {% endif %}
  {% endfor %}

  {% capture log_message %}
    {{ products_count }} published products scanned, {{ updated_products_count }} products ({{ updated_variants_count }} variants) required an update
  {% endcapture %}

  {"log": {{ log_message | strip | json }}}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more