Maintain a collection of new products, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Maintain a collection of new products

by Isaac Bowen (team@usemechanic.com)

Use this task to maintain a "New Products" collection, specifying either a number of products to include or the number of days to keep each product around. Easy! :)

Runs when a user triggers the task and every day at midnight. Configuration includes number of days to keep a product in this collection, number of products to keep in this collection, and collection.

15-day free trial – unlimited tasks

Documentation

To use this task, create a manual collection, and add the collection ID to the task options. (Find the ID by opening the collection in the Shopify admin, then looking at the URL in your browser's address bar. If the URL is https://example.myshopify.com/admin/collections/12345, the collection ID is 12345.)

Use the "Run task" button to populate your collection for the first time. After that, this task will run daily, at midnight in your local timezone. During each run, the task will update the collection, adding new products and removing old ones as appropriate.

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 a user triggers the task (mechanic/user/trigger)
every day at midnight (mechanic/scheduler/daily)
Options
number of days to keep a product in this collection (number), number of products to keep in this collection (number), collection (number, required)
Script
{% assign days_to_keep_products = options.number_of_days_to_keep_a_product_in_this_collection__number %}
{% assign product_count = options.number_of_products_to_keep_in_this_collection__number %}

{% if event.preview %}
  {% if days_to_keep_products == blank and product_count == blank %}
    {"error": "Please fill in either \"Number of days to keep a product in this collection\" or \"Number of products to keep in this collection\" (but not both!)."}
  {% elsif days_to_keep_products != blank and product_count != blank %}
    {"error": "Please choose one of the options, but not both! :)"}
  {% else %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "create",
          "collect",
          {
            "collection_id": 1234567890,
            "product_id": 1234567890
          }
        ]
      }
    }

    {
      "action": {
        "type": "shopify",
        "options": [
          "delete",
          [
            "collect",
            1234567890
          ]
        ]
      }
    }
  {% endif %}
{% else %}
  {% assign sorted_products = shop.products.published | sort: "created_at" | reverse %}
  {% assign final_products = nil %}

  {% if product_count != blank %}
    {% assign final_products = sorted_products | slice: 0, product_count %}
  {% elsif days_to_keep_products != blank %}
    {% assign now_s = "now" | date: "%s" | times: 1 %}
    {% assign days_to_keep_products_s = days_to_keep_products | times: 24 | times: 60 | times: 60 %}
    {% assign created_at_threshold_s = now_s | minus: days_to_keep_products_s %}

    {% assign product_count_by_age = nil %}
    {% for product in sorted_products %}
      {% assign product_created_at_s = product.created_at | date: "%s" | times: 1 %}
      {% if product_created_at_s < created_at_threshold_s %}
        {% assign product_count_by_age = forloop.index0 %}
        {% break %}
      {% endif %}
    {% endfor %}

    {% if product_count_by_age == nil %}
      {% assign final_products = sorted_products %}
    {% else %}
      {% assign final_products = sorted_products | slice: 0, product_count_by_age %}
    {% endif %}
  {% endif %}

  {% assign collection = shop.collections[options.collection_id__number_required] %}
  {% assign current_collects = collection.collects %}

  {% assign current_product_ids = current_collects | map: "product_id" %}

  {% assign product_ids_to_keep = "[]" | parse_json %}
  {% for product in final_products %}
    {% if current_product_ids contains product.id %}
      {% assign product_id_array = "[" | append: product.id | append: "]" | parse_json %}
      {% assign product_ids_to_keep = product_ids_to_keep | concat: product_id_array %}
    {% endif %}
  {% endfor %}

  {% for collect in current_collects %}
    {% if product_ids_to_keep contains collect.product_id %}
      {% continue %}
    {% endif %}

    {
      "action": {
        "type": "shopify",
        "options": [
          "delete",
          [
            "collect",
            {{ collect.id | json }}
          ]
        ]
      }
    }
  {% endfor %}

  {% for product in final_products %}
    {% if product_ids_to_keep contains product.id %}
      {% continue %}
    {% endif %}

    {
      "action": {
        "type": "shopify",
        "options": [
          "create",
          "collect",
          {
            "collection_id": {{ collection.id | json }},
            "product_id": {{ product.id | json }}
          }
        ]
      }
    }
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.