Tag orders that have at least x of a certain product, with Mechanic.

Mechanic is a development platform for Shopify. :)

Tag orders that have at least x of a certain product

Use this task to monitor for orders that meet a certain quantity threshold, per line item or totaled up, optionally only looking at certain SKUs. Optionally, send yourself an email notification when such an order comes in.

Runs when an order is paid. Configuration includes minimum quantity threshold, evaluate threshold per line item, only monitor these skus, order tag to apply, and email notification recipient.

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.

Open source
View on GitHub to contribute to this task
Events
when an order is paid (shopify/orders/paid)
Options
minimum quantity threshold (number, required), evaluate threshold per line item (boolean), only monitor these skus (array), order tag to apply, email notification recipient (email)
Script
{% comment %}
  Option order:

  {{ options.minimum_quantity_threshold__number_required }}
  {{ options.evaluate_threshold_per_line_item__boolean }}
  {{ options.only_monitor_these_skus__array }}
  {{ options.order_tag_to_apply }}
  {{ options.email_notification_recipient__email }}
{% endcomment %}

{% if event.preview %}
  {% capture order_json %}
    {
      "id": 1234567890,
      "admin_graphql_api_id": "gid://shopify/Order/1234567890",
      "name": "#1234",
      "line_items": [
        {
          "sku": {{ options.only_monitor_these_skus__array[0] | default: "ABC123" | json }},
          "quantity": {{ options.minimum_quantity_threshold__number_required | json }},
          "title": "Short sleeve t-shirt"
        }
      ]
    }
  {% endcapture %}

  {% assign order = order_json | parse_json %}
{% endif %}

{% if options.order_tag_to_apply == blank and options.email_notification_recipient__email == blank %}
  {% error "Fill in at least one of 'Order tag to apply' and 'Email notification recipient'. :)" %}
{% endif %}

{% assign line_item_summaries = array %}
{% assign skus = array %}
{% assign total_quantity = 0 %}
{% assign total_line_items = 0 %}
{% assign order_qualifies = false %}

{% for line_item in order.line_items %}
  {% if options.only_monitor_these_skus__array == blank or options.only_monitor_these_skus__array 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 line_item.quantity >= options.minimum_quantity_threshold__number_required %}
        {% assign line_item_qualifies = true %}
      {% endif %}
    {% else %}
      {% assign line_item_qualifies = true %}
    {% endif %}

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

      {% unless options.only_monitor_these_skus__array == blank %}
        {% assign skus[skus.size] = line_item.sku %}
      {% endunless %}

      {% assign line_item_summaries[line_item_summaries.size] = line_item.quantity | append: "x " | append: line_item.sku | append: " (" | append: line_item.title | append: ")" %}
    {% endif %}
  {% endif %}
{% endfor %}

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

{% if order_qualifies %}
  {% if options.order_tag_to_apply != blank %}
    {% action "shopify" %}
      mutation {
        tagsAdd(
          id: {{ order.admin_graphql_api_id | json }}
          tags: {{ options.order_tag_to_apply | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endif %}

  {% if options.email_notification_recipient__email != blank %}
    {% capture at_least_x -%}
      at least {{ options.minimum_quantity_threshold__number_required }} {% if total_line_items > 1 %}{% if options.evaluate_threshold_per_line_item__boolean %} each{% else %}total{% endif %}{% endif %}
    {%- endcapture %}

    {% capture email_body %}
      Hi there,

      Order {{ order.name }} contained {{ at_least_x }} of {{ total_line_items | pluralize: "this item", "these items" }}:

      {{ line_item_summaries | join: "<br>" }}

      See this order in Shopify:

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

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

    {% if options.only_monitor_these_skus__array == blank %}
      {% assign qualifying_items_addendum = "items" %}
    {% else %}
      {% assign qualifying_items_addendum = skus | join: ", " | prepend: "of " %}
    {% endif %}

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