Mechanic is a development and ecommerce automation platform for Shopify. :)
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 Occurs when a user manually triggers the task and Occurs every day at midnight (in local time). Configuration includes collection id, number of days to keep a product in this collection, and number of products to keep in this collection.
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! :)
To use this task, create a manual collection, and add the collection ID to the task options. (See finding a resource ID for help locating the collection ID.)
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.
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?)
mechanic/user/trigger mechanic/scheduler/daily
{% assign collection_id = options.collection_id__number_required | prepend: "gid://shopify/Collection/" %} {% 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 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!)." {% enderror %} {% elsif days_to_keep_products != blank and product_count != blank %} {% error "Please choose one of the options, but not both! :)" %} {% endif %} {% comment %} -- get IDs of all products currently in the collection {% endcomment %} {% assign cursor = nil %} {% assign current_product_ids = array %} {% for n in (1..100) %} {% capture query %} query { collection(id: {{ collection_id | json }}) { products( first: 250 after: {{ cursor | json }} ) { pageInfo { hasNextPage endCursor } nodes { id } } } } {% endcapture %} {% assign result = query | shopify %} {% if event.preview %} {% capture result_json %} { "data": { "collection": { "products": { "nodes": [ { "id": "gid://shopify/Product/1234567890" } ] } } } } {% endcapture %} {% assign result = result_json | parse_json %} {% endif %} {% assign current_product_ids = result.data.collection.products.nodes | map: "id" | concat: current_product_ids %} {% if result.data.collection.products.pageInfo.hasNextPage %} {% assign cursor = result.data.collection.products.pageInfo.endCursor %} {% else %} {% break %} {% endif %} {% endfor %} {% assign loop_count = 100 %} {% assign first_elements = 250 %} {% assign products_query = "status:active" %} {% if days_to_keep_products != blank %} {% comment %} -- calculate the "created at" threshold for products to keep in the collection {% endcomment %} {% if days_to_keep_products < 1 %} {% error %} "\"Number of days to keep a product in this collection\" should be > 0" {% enderror %} {% endif %} {% assign now_s = "now" | date: "%s" | times: 1 %} {% assign days_to_keep_products_s = days_to_keep_products | times: 86400 %} {% assign created_at_threshold_s = now_s | minus: days_to_keep_products_s %} {% assign created_at_threshold = created_at_threshold_s | date: "%Y-%m-%d" %} {% assign products_query = products_query | append: " created_at:>=" | append: created_at_threshold %} {% elsif product_count != blank %} {% comment %} -- calculate the product pagination loops based on number of products to keep in the collection {% endcomment %} {% if product_count < 1 %} {% error %} "\"Number of products to keep in this collection\" should be > 0" {% enderror %} {% endif %} {% assign loop_count = product_count | divided_by: 250 %} {% assign remaining = product_count | modulo: 250 %} {% endif %} {% comment %} -- get qualifying product IDs based on chosen criteria {% endcomment %} {% assign cursor = nil %} {% assign qualifying_product_ids = array %} {% for n in (0..loop_count) %} {% if product_count != blank %} {% if forloop.last %} {% if remaining == 0 %} {% break %} {% endif %} {% assign first_elements = remaining %} {% endif %} {% endif %} {% capture query %} query { products( first: {{ first_elements }} after: {{ cursor | json }} query: {{ products_query | json }} reverse: true sortKey: CREATED_AT ) { pageInfo { hasNextPage endCursor } nodes { id } } } {% endcapture %} {% assign result = query | shopify %} {% if event.preview %} {% capture result_json %} { "data": { "products": { "nodes": [ { "id": "gid://shopify/Product/2345678901" } ] } } } {% endcapture %} {% assign result = result_json | parse_json %} {% endif %} {% assign result_product_ids = result.data.products.nodes | map: "id" %} {% assign qualifying_product_ids = qualifying_product_ids | concat: result_product_ids %} {% if result.data.products.pageInfo.hasNextPage %} {% assign cursor = result.data.products.pageInfo.endCursor %} {% else %} {% break %} {% endif %} {% endfor %} {% comment %} -- create arrays for the products to add and to remove, and split them into groups of 250 {% endcomment %} {% assign product_ids_to_add = array %} {% assign product_ids_to_remove = array %} {% for qualifying_product_id in qualifying_product_ids %} {% unless current_product_ids contains qualifying_product_id %} {% assign product_ids_to_add = product_ids_to_add | push: qualifying_product_id %} {% endunless %} {% endfor %} {% for current_product_id in current_product_ids %} {% unless qualifying_product_ids contains current_product_id %} {% assign product_ids_to_remove = product_ids_to_remove | push: current_product_id %} {% endunless %} {% endfor %} {% assign mutations = array %} {% if product_ids_to_add != blank %} {% assign groups_of_product_ids_to_add = product_ids_to_add | in_groups_of: 250, fill_with: false %} {% for group_of_product_ids_to_add in groups_of_product_ids_to_add %} {% capture mutation %} collectionAddProducts{{ forloop.index0 }}: collectionAddProducts( id: {{ collection_id | json }} productIds: {{ group_of_product_ids_to_add | json }} ) { userErrors { field message } } {% endcapture %} {% assign mutations = mutations | push: mutation %} {% endfor %} {% endif %} {% if product_ids_to_remove != blank %} {% assign groups_of_product_ids_to_remove = product_ids_to_remove | in_groups_of: 250, fill_with: false %} {% for group_of_product_ids_to_remove in groups_of_product_ids_to_remove %} {% capture mutation %} collectionRemoveProducts{{ forloop.index0 }}: collectionRemoveProducts( id: {{ collection_id | json }} productIds: {{ group_of_product_ids_to_remove | json }} ) { userErrors { field message } } {% endcapture %} {% assign mutations = mutations | push: mutation %} {% endfor %} {% endif %} {% if mutations != blank %} {% action "shopify" %} mutation { {{ mutations | join: newline }} } {% endaction %} {% else %} {% log "This collection already has the correct products." %} {% endif %}