Mechanic is a development and ecommerce automation platform for Shopify. :)
Use this task to schedule adding and/or removing tags for specific products, with support for scheduling by date, or by date and hour.
Runs Occurs every day at midnight (in local time) and Occurs when a user manually triggers the task. Configuration includes dates and tags to add, dates and tags to remove, product ids, product tag to monitor, use hour specificity, and test mode.
Use this task to schedule adding and/or removing tags for specific products, with support for scheduling by date, or by date and hour.
Either use the "Product IDs" option to choose which products this task will tag, or enter a "Product tag to monitor". If you need to monitor more than 250 products, then you must use the "Product tag to monitor" option. Learn how to find product IDs here.
Fill in the "Dates and tags to add" and/or "Dates and tags to remove" options with dates (including year, month, and day) on the left, and product tags on the right.
Enable "Use hour specificity" to have Mechanic schedule tags by date and hour, instead of only by date. When this option is enabled, fill in tag dates using an hour at the end, e.g. "2019-01-01 2PM". A date without an hour will be interpreted as midnight at the beginning of that date - for example, "2019-01-01" will be interpreted as "2019-01-01 12AM".
When "Test mode" is enabled, use the "Run task" button to see a summary of Mechanic's understanding of your configuration. Mechanic will not perform any tagging or untagging when this mode is enabled.
This task will run nightly, at midnight in your store's local timezone. If "Use hour specificity" is enabled, the task will run hourly instead. You can also run this task manually, using the "Run task" button, and the task will perform the operations appropriate for the current day (or hour, if "Use hour specificity" is enabled).
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?)
{% unless options.test_mode__boolean %} {% if options.use_hour_specificity__boolean %} mechanic/scheduler/hourly {% else %} mechanic/scheduler/daily {% endif %} {% endunless %} mechanic/user/trigger
{% assign dates_and_tags_to_add = options.dates_and_tags_to_add__keyval %} {% assign dates_and_tags_to_remove = options.dates_and_tags_to_remove__keyval %} {% assign product_ids = options.product_ids__array_number %} {% assign product_tag_to_monitor = options.product_tag_to_monitor %} {% assign use_hour_specificity = options.use_hour_specificity__boolean %} {% assign test_mode = options.test_mode__boolean %} {% if dates_and_tags_to_add == blank and dates_and_tags_to_remove == blank %} {% error "Fill in at least one of 'Dates and tags to add' and 'Dates and tags to remove'. :)" %} {% endif %} {% if product_ids == blank and product_tag_to_monitor == blank %} {% error "Fill in either 'Product IDs' or 'Product tag to monitor'." %} {% elsif product_ids != blank and product_tag_to_monitor != blank %} {% error "Fill in either 'Product IDs' or 'Product tag to monitor', but not both." %} {% endif %} {% if product_ids.size > 250 %} {% log product_ids_count: product_ids.size %} {% error "If you need to enter more than 250 product IDs, use the 'Product tag to monitor' option instead." %} {% endif %} {% comment %} -- determine which tags should be added and/or removed on this task run {% endcomment %} {% assign configured_tags_to_add = array %} {% assign configured_tags_to_remove = array %} {% if use_hour_specificity %} {% assign time_format = "%Y-%m-%d %I%p" %} {% else %} {% assign time_format = "%Y-%m-%d" %} {% endif %} {% assign now_time = "now" | date: time_format %} {% assign full_schedule = hash %} {% for keyval in dates_and_tags_to_add %} {% assign some_time = keyval[0] | date: time_format %} {% if some_time == now_time %} {% assign configured_tags_to_add = configured_tags_to_add | push: keyval[1] %} {% endif %} {% if full_schedule[some_time] == nil %} {% assign full_schedule[some_time] = array %} {% endif %} {% assign n = full_schedule[some_time].size %} {% assign full_schedule[some_time][n] = "add: " | append: keyval[1] %} {% endfor %} {% for keyval in dates_and_tags_to_remove %} {% assign some_time = keyval[0] | date: time_format %} {% if some_time == now_time %} {% assign configured_tags_to_remove = configured_tags_to_remove | push: keyval[1] %} {% endif %} {% if full_schedule[some_time] == nil %} {% assign full_schedule[some_time] = array %} {% endif %} {% assign n = full_schedule[some_time].size %} {% assign full_schedule[some_time][n] = "remove: " | append: keyval[1] %} {% endfor %} {% if event.preview %} {% comment %} -- add preview values for tags since configured dates are unlikely to align when task is previewed {% endcomment %} {% assign configured_tags_to_add[0] = "preview-add" %} {% assign configured_tags_to_remove[0] = "preview-remove" %} {% endif %} {% if configured_tags_to_add == blank and configured_tags_to_remove == blank %} {% log message: "No tags to add or remove on this task run based on configured schedule.", now: now_time, schedule: full_schedule %} {% break %} {% endif %} {% if product_ids != blank %} {% assign search_query_parts = array %} {% for product_id in product_ids %} {% assign search_query_parts[search_query_parts.size] = product_id | prepend: "id:" %} {% endfor %} {% assign search_query = search_query_parts | join: " OR " %} {% else %} {% assign search_query = product_tag_to_monitor | json | prepend: "tag:" %} {% endif %} {% comment %} -- get data for the products (up to 25K) {% endcomment %} {% assign cursor = nil %} {% assign products = array %} {% for n in (1..100) %} {% capture query %} query { products( first: 250 after: {{ cursor | json }} query: {{ search_query | json }} ) { pageInfo { hasNextPage endCursor } nodes { id handle tags } } } {% endcapture %} {% assign result = query | shopify %} {% if event.preview %} {% capture result_json %} { "data": { "products": { "nodes": [ { "id": "gid://shopify/Product/1234567890", "handle": "preview-product" } ] } } } {% endcapture %} {% assign result = result_json | parse_json %} {% endif %} {% assign products = products | concat: result.data.products.nodes %} {% if result.data.products.pageInfo.hasNextPage %} {% assign cursor = result.data.products.pageInfo.endCursor %} {% else %} {% break %} {% endif %} {% endfor %} {% assign product_handles = products | map: "handle" | sort %} {% log now: now_time, schedule: full_schedule, product_tag_to_monitor: product_tag_to_monitor, configured_product_ids: product_ids, matched_product_handles: product_handles %} {% if test_mode %} {% break %} {% endif %} {% for product in products %} {% assign tags_to_add = array %} {% assign tags_to_remove = array %} {% for tag in configured_tags_to_add %} {% unless product.tags contains tag %} {% assign tags_to_add = tags_to_add | push: tag %} {% endunless %} {% endfor %} {% for tag in configured_tags_to_remove %} {% if product.tags contains tag %} {% assign tags_to_remove = tags_to_remove | push: tag %} {% endif %} {% endfor %} {% if tags_to_add != blank or tags_to_remove != blank %} {% action "shopify" %} mutation { {% if tags_to_add != blank %} tagsAdd( id: {{ product.id | json }} tags: {{ tags_to_add | json }} ) { userErrors { field message } } {% endif %} {% if tags_to_remove != blank %} tagsRemove( id: {{ product.id | json }} tags: {{ tags_to_remove | json }} ) { userErrors { field message } } {% endif %} } {% endaction %} {% endif %} {% endfor %}
{"2019-08-24" => "saturday", "2019-08-23" => "friday"}
{"2019-08-24" => "friday", "2019-08-23" => "thursday"}
["1234567890"]