Tag orders that have more than x of a certain product with Mechanic.

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

Tag orders that have more than x of a certain product

by Isaac Bowen (team@usemechanic.com)

Use this task to monitor for orders that meet a certain quantity threshold of a certain product, configured by SKU. Optionally, send yourself an email notification when such an order comes in.

Runs when an order is paid. Configuration includes skus to monitor, evaluate threshold per line item, quantity threshold for tagging, tag to apply, and staff notification email.

15-day free trial – unlimited tasks

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 an order is paid (shopify/orders/paid)
Options
skus to monitor (array, required), evaluate threshold per line item (boolean), quantity threshold for tagging (number, required), tag to apply, staff notification email (email)
Script
{% assign sku_summaries = "" %}
{% assign skus = "" %}
{% assign total_quantity = 0 %}
{% assign total_skus = 0 %}
{% assign order_qualifies = false %}

{% for line_item in order.line_items %}
  {% if event.preview or options.skus_to_monitor__array_required contains line_item.sku %}
    {% assign total_quantity = total_quantity | plus: line_item.quantity %}

    {% assign line_item_qualifies = false %}
    {% if options.evaluate_threshold_per_line_item__boolean %}
      {% if event.preview or line_item.quantity >= options.quantity_threshold_for_tagging__number_required %}
        {% assign line_item_qualifies = true %}
      {% endif %}
    {% else %}
      {% assign line_item_qualifies = true %}
    {% endif %}

    {% if line_item_qualifies %}
      {% assign total_skus = total_skus | plus: 1 %}

      {% capture skus %}{% if skus != blank %}{{ skus }}, {% endif %}{{ line_item.sku }}{% endcapture %}

      {% assign sku_summary = line_item.quantity | append: "x " | append: line_item.sku | append: " (" | append: line_item.title | append: ")" %}
      {% capture sku_summaries %}{% if sku_summaries != blank %}{{ sku_summaries }}, {% endif %}{{ sku_summary }}{% endcapture %}
    {% endif %}
  {% endif %}
{% endfor %}

{% if options.evaluate_threshold_per_line_item__boolean %}
  {% if skus != blank %}
    {% assign order_qualifies = true %}
  {% endif %}
{% else %}
  {% if total_quantity >= options.quantity_threshold_for_tagging__number_required %}
    {% assign order_qualifies = true %}
  {% endif %}
{% endif %}

{% if event.preview %}
  {% assign overage_skus = options.skus_to_monitor__array_required | join: ", " %}
  {% capture overages %}{% for sku in options.skus_to_monitor__array_required %}{{ options.quantity_threshold_for_tagging__number_required }}x {{ sku }}{% unless forloop.last %}, {% endunless %}{% endfor %}{% endcapture %}
{% endif %}

{% if event.preview or order_qualifies %}
  {% if options.tag_to_apply != blank %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "update",
          [
            "order",
            {{ order.id | json }}
          ],
          {
            "tags": {{ order.tags | add_tag: options.tag_to_apply | json }}
          }
        ]
      }
    }
  {% endif %}

  {% if options.staff_notification_email__email != blank %}
    {% capture at_least_x_of -%}
      at least {{ options.quantity_threshold_for_tagging__number_required }} {% if total_skus > 1 %}{% if options.evaluate_threshold_per_line_item__boolean %}of each{% else %}total{% endif %}{% endif %} of
    {%- endcapture %}

    {% capture email_body %}
      Hi there,

      Order {{ order.name }} contained {{ at_least_x_of }} {{ total_skus | pluralize: "this item", "these items" }}:

      {{ sku_summaries | split: ", " | join: "<br>" }}

      See this order in Shopify:

      https://{{ shop.domain }}/admin/orders/{{ order.id }}

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

    {
      "action": {
        "type": "email",
        "options": {
          "to": {{ options.staff_notification_email__email | json }},
          "subject": {{ "Order " | append: order.name | append: " contained " | append: at_least_x_of | append: " " | append: skus | json }},
          "body": {{ email_body | unindent | newline_to_br | json }},
          "reply_to": {{ shop.customer_email | json }},
          "from_display_name": {{ shop.name | json }}
        }
      }
    }
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.