Mechanic Get Mechanic
Task library

Maintain a collection of recently purchased products

This task watches for newly-paid orders (or newly-fulfilled orders, if you choose), and adds or moves the purchased products to the top of the collection of your choice. Optionally, ignore products that are sold out or that have one of a list of certain tags. Useful for "trending products" functionality. :)

Try this task

When it runs

Occurs whenever an order is paid

What you choose

  • Collection maximum size
  • Collection
  • Exclude products that are sold out
  • Exclude products with any of these tags
  • Wait until order is fulfilled

Documentation

This task watches for newly-paid orders (or newly-fulfilled orders, if you choose), and adds or moves the purchased products to the top of the collection of your choice. Optionally, ignore products that are sold out or that have one of a list of certain tags. Useful for "trending products" functionality. :)

Configure this task with a collection which either has no sources or conditions, or only has products manually added. The collection must also be configured for manual sorting or the task will not be able to add the most recent purchases to the top of the collection.

IMPORTANT:
- This task must use the 2026-07 API or newer
- Do not add additional sources or conditions to the configured collection as that may prevent this task from being able to maintain it

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

{% if options.wait_until_order_is_fulfilled__boolean %}
  shopify/orders/fulfilled
{% else %}
  shopify/orders/paid
{% endif %}
How task subscriptions work

Option types and flags

Task code

{% assign collection_maximum_size = options.collection_maximum_size__number_required %}
{% assign collection_id = options.collection__picker_collection_required %}
{% assign exclude_products_that_are_sold_out = options.exclude_products_that_are_sold_out__boolean %}
{% assign exclude_product_tags = options.exclude_products_with_any_of_these_tags__array %}

{% assign exclude_product_tags_lower = exclude_product_tags | json | downcase | parse_json %}

{% if collection_maximum_size < 1 %}
  {% error "The 'Collection maximum size' option must be at least 1. :)" %}

{% else %}
  {% assign collection_maximum_size_rounded = collection_maximum_size | round %}

  {% if collection_maximum_size_rounded != collection_maximum_size %}
    {% error "The 'Collection maximum size' option must be a whole number. ;)" %}
  {% endif %}
{% endif %}

{% comment %}
  -- get product IDs from order line items (limit of 500 line items per order)
{% endcomment %}

{% assign product_ids_ordered = array %}
{% assign cursor = nil %}

{% for n in (1..2) %}
  {% capture query %}
    query {
      order(id: {{ order.admin_graphql_api_id | json }}) {
        id
        lineItems(
          first: 250
          after: {{ cursor | json }}
        ) {
          pageInfo {
            hasNextPage
            endCursor
          }
          nodes {
            product {
              id
              totalInventory
              tags
            }
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "order": {
            "id": "gid://shopify/Order/1234567890",
            "lineItems": {
              "nodes": [
                {
                  "product": {
                    "id": "gid://shopify/Product/1234567890",
                    "totalInventory": 1
                  }
                }
              ]
            }
          }
        }
      }
    {% endcapture %}

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

  {% comment %}
    -- exclude line items without products, and optionally slod-out products and products with certain tags
  {% endcomment %}

  {% for line_item in result.data.order.lineItems.nodes %}
    {% assign product = line_item.product %}

    {% if product == blank %}
      {% continue %}
    {% endif %}

    {% assign skip = nil %}

    {% if exclude_products_that_are_sold_out and product.totalInventory <= 0 %}
      {% assign skip = true %}

    {% elsif exclude_product_tags != blank %}
      {% for product_tag in product.tags %}
        {% assign product_tag_lower = product_tag | downcase %}

        {% if exclude_product_tags_lower contains product_tag_lower %}
          {% assign skip = true %}
          {% break %}
        {% endif %}
      {% endfor %}
    {% endif %}

    {% if skip %}
      {% continue %}
    {% endif %}

    {% assign product_ids_ordered = product_ids_ordered | push: product.id %}
  {% endfor %}

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

{% comment %}
  -- get existing products in collection, also applying optional exclusions
{% endcomment %}

{% assign product_ids_already_collected = array %}
{% assign cursor = nil %}
{% assign collection_sort_order = nil %}
{% assign product_ids_to_remove = array %}

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

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "collection": {
            "id": "gid://shopify/Collection/1234567890",
            "sortOrder": "MANUAL",
            "products": {
              "nodes": [
                {
                  "id": "gid://shopify/Product/2345678901",
                  "totalInventory": 1
                }
              ]
            }
          }
        }
      }
    {% endcapture %}

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

  {% assign collection_sort_order = result.data.collection.sortOrder %}

  {% for product in result.data.collection.products.nodes %}
    {% assign skip = nil %}

    {% if exclude_products_that_are_sold_out and product.totalInventory <= 0 %}
      {% assign skip = true %}

    {% elsif exclude_product_tags != blank %}
      {% for product_tag in product.tags %}
        {% assign product_tag_lower = product_tag | downcase %}

        {% if exclude_product_tags_lower contains product_tag_lower %}
          {% assign skip = true %}
          {% break %}
        {% endif %}
      {% endfor %}
    {% endif %}

    {% if skip %}
      {% assign product_ids_to_remove = product_ids_to_remove | push: product.id %}
    {% else %}
      {% assign product_ids_already_collected = product_ids_already_collected | push: product.id %}
    {% endif %}
  {% endfor %}

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

{% comment %}
  -- figure out which products should be in the collection to keep it within maximum size
{% endcomment %}

{% assign product_ids_under_consideration = product_ids_ordered | concat: product_ids_already_collected | uniq %}
{% assign product_ids_should_have = array %}
{% assign product_ids_to_add = array %}

{% for product_id in product_ids_under_consideration %}
  {% if forloop.index <= collection_maximum_size %}
    {% assign product_ids_should_have = product_ids_should_have | push: product_id %}

    {% unless product_ids_already_collected contains product_id %}
      {% assign product_ids_to_add = product_ids_to_add | push: product_id %}
    {% endunless %}

  {% else %}
    {% assign product_ids_to_remove = product_ids_to_remove | push: product_id %}
  {% endif %}
{% endfor %}

{% comment %}
  -- capture any necessary mutations
{% endcomment %}

{% assign mutations = array %}

{% if product_ids_to_add != blank %}
  {% capture mutation %}
    collectionAddProducts(
      id: {{ collection_id | json }}
      productIds: {{ product_ids_to_add | json }}
    ) {
      userErrors {
        field
        message
      }
    }
  {% endcapture %}

  {% assign mutations = mutations | push: mutation %}
{% endif %}

{% if product_ids_to_remove != blank %}
  {% capture mutation %}
    collectionRemoveProducts(
      id: {{ collection_id | json }}
      productIds: {{ product_ids_to_remove | json }}
    ) {
      userErrors {
        field
        message
      }
    }
  {% endcapture %}

  {% assign mutations = mutations | push: mutation %}
{% endif %}

{% comment %}
  -- only sort the collection if it is configured for manual sorting
{% endcomment %}

{% if collection_sort_order == "MANUAL" %}
  {% assign moves = array %}

  {% for product_id in product_ids_ordered %}
    {% if product_ids_already_collected[forloop.index0] != product_id %}
      {% assign move = hash %}
      {% assign move["id"] = product_id %}
      {% assign move["newPosition"] = forloop.index0 | append: "" %}
      {% assign moves = moves | push: move %}
    {% endif %}
  {% endfor %}

  {% if moves != blank %}
    {% capture mutation %}
      collectionReorderProducts(
        id: {{ collection_id | json }}
        moves: {{ moves | graphql_arguments }}
      ) {
        userErrors {
          field
          message
        }
      }
    {% endcapture %}

    {% assign mutations = mutations | push: mutation %}
  {% endif %}

{% else %}
  {% action "echo" __error: "This collection is not configured for manual sorting. This task will not be able to move the most recent purchases to the top of the collection." %}
{% endif %}

{% if mutations != blank %}
  {% action "shopify" %}
    mutation {
      {{ mutations | join: newline }}
    }
  {% endaction %}
{% endif %}
Mechanic Liquid reference

Default option values

Exclude products that are sold out
false