Advanced: Scheduled Price Changes
This advanced task allows you to schedule price change events for your store, useful for seasonal and flash sales. To use this task, configure a price change event using the task options, save the task and run it manually, and then enter a keyword of schedule in the dialog that appears. An email summary with the price change event ID will be sent upon successful scheduling.
Try this taskWhen it runs
- Occurs when a user manually triggers the task with text
- Occurs whenever user/price_changes/start is triggered
- Occurs whenever user/price_changes/end is triggered
- Occurs whenever user/price_changes/reset is triggered
What you choose
- Notification email recipients
- Event start datetime
- Event end datetime
Show 6 more
- Set compare at prices to original price during event
- Collection handles and discounts
- SKUs to include
- SKU discount
- Exclude products tagged with
- SKUs to exclude
Documentation
This advanced task allows you to schedule price change events for your store, useful for seasonal and flash sales. To use this task, configure a price change event using the task options, save the task and run it manually, and then enter a keyword of schedule in the dialog that appears. An email summary with the price change event ID will be sent upon successful scheduling.
This task supports these keyword actions:
- schedule - Validates and schedules a new price change event using the current task configuration.
- list - Logs out (in Mechanic) all configured price change events (of any status).
- email - Emails a summary of all configured price change events (of any status).
- cancel - Cancels a specific scheduled or ongoing price change event. The cancel keyword must be followed by a space and the price change event ID.
- reset - Clears all price change events (of any status) from the shop metafield, and reverts the prices on any variants that have a price change event applied.
Key features of this task:
- Ability to schedule overlapping price change events
- Ability to cancel an ongoing event
- Ability to add collections that contain more than 1000 products
- Ability to discount more than 20k products
- Supports percentage, fixed price, and fixed discount options for products AND collections
- Option to include specific SKUs in a list with a specific discount for the whole list
- Option to exclude SKUs from any discount qualification
- Option to exclude products by tag from any discount qualification
- Option to have (or not have) compare at prices set to original prices for the duration of an event
Price change event configuration notes
- The event start and end datetimes must be valid dates in the form of YYYY-MM-DD or YYYY-MM-DD HH:MM (24 hour clock).
- Collection handles and discounts are entered as key value pairs, with the collection handle on the left and the discount for it on the right.
- If any SKUs are specifically included, then a SKU discount must be set to apply to them.
- Discount formats:
- Percentage - This must be an integer between 1 and 99, followed immediately by % (e.g. 20%)
- Fixed price - Any positive integer or float will be considered the fixed price that all applicable items will be discounted to (e.g. 123.45)
- Fixed discount - Any negative integer or float will be considered the discount to be substracted from the price of all applicable items (e.g. -25)
- Priority of discount assignment when a variant qualifies for multiple discounts within a price change event:
- Exclusions (by SKU or product tag) will always override inclusion
- SKU inclusion list
- Collection membership in order of configuration
Important Notes:
- An email notification will be sent for every price change event scheduled, started, completed, and cancelled. If an email is not received as expected, then the task run logs should be reviewed.
- Price change events that affect a very large number of variants will take some time to process all of the price updates. The notification emails are sent after all variant prices have been updated.
- Just saving the task configuration will not schedule (or validate) a price change event. This must be done with the "schedule" keyword when run manually.
- This task only manages variant prices. Product and theme publishing are not handled by this task.
- Variants that are already part of an ongoing price change event will not be modified if targeted by a new price change event.
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
mechanic/user/text
user/price_changes/start
user/price_changes/end
user/price_changes/reset
How task subscriptions work
Option types and flags
- Notification email recipients (array, required)
- Event start datetime (required)
- Event end datetime (required)
- Set compare at prices to original price during event (boolean)
- Collection handles and discounts (keyval)
- SKUs to include (array)
- Exclude products tagged with (array)
- SKUs to exclude (array)
Task code
{% assign shop_metafield_namespace = "mechanic" %}
{% assign shop_metafield_key = "price_change_events" %}
{% assign variant_metafield_namespace = "mechanic" %}
{% assign variant_metafield_key = "price_change_event" %}
{% assign email_recipients = options.notification_email_recipients__array_required %}
{% capture task_admin_link -%}
<a href="https://{{ shop.myshopify_domain }}/admin/apps/mechanic/~/tasks/edit/{{ task.id }}">{{ task.name | remove: "ADVANCED: " }}</a>
{%- endcapture %}
{% comment %}
-- query for shop data on every task run, since it will be needed for each valid action keyword and custom event
{% endcomment %}
{% capture shop_query %}
query {
shop {
id
metafield(
namespace: {{ shop_metafield_namespace | json }}
key: {{ shop_metafield_key | json }}
) {
id
jsonValue
}
}
}
{% endcapture %}
{% assign shop_result = shop_query | shopify %}
{% if event.preview %}
{% capture shop_result_json %}
{
"data": {
"shop": {
"id": "gid://shopify/Shop/1234567890",
"metafield": {
"id": "gid://shopify/Metafield/9876543210",
"jsonValue": {
"price_change_event__1234567890": {
"status": "scheduled",
"start": "2021-12-30 08:00",
"end": "2021-12-31 20:00",
"set_compare_at_prices": true,
"collection_handles_and_discounts": {
"collection-alpha": "20%",
"collection-beta": "-10",
"collection-gamma": "20"
},
"skus_to_include": [],
"sku_discount": "",
"skus_to_exclude": [],
"exclude_products_tagged_with": [
"clearance"
]
}
}
}
}
}
}
{% endcapture %}
{% assign shop_result = shop_result_json | parse_json %}
{% endif %}
{% assign shop = shop_result.data.shop %}
{% assign price_change_events = shop.metafield.jsonValue | default: hash %}
{% if event.topic == "mechanic/user/text" %}
{% comment %}
-- check text entry to see what action to take
{% endcomment %}
{% assign action_keyword = event.data | downcase %}
{% if event.preview %}
{% assign action_keyword = "schedule" %}
{% endif %}
{% case action_keyword %}
{% when "schedule" %}
{% comment %}
-- NOTE: run scheduling logic here instead of in custom event like most other keywords, so user gets immediate feedback on any configuration errors
{% endcomment %}
{% assign valid_start_datetime = options.event_start_datetime__required | parse_date: "%Y-%m-%d %H:%M" %}
{% if valid_start_datetime == blank %}
{% assign valid_start_datetime = options.event_start_datetime__required | parse_date: "%Y-%m-%d" %}
{% if valid_start_datetime == blank %}
{% unless event.preview %}
{% error "The event start date is not a valid date. Please re-enter it (per the task documentation) and try scheduling the event again." %}
{% break %}
{% endunless %}
{% endif %}
{% endif %}
{% assign valid_end_datetime = options.event_end_datetime__required | parse_date: "%Y-%m-%d %H:%M" %}
{% if valid_end_datetime == blank %}
{% assign valid_end_datetime = options.event_end_datetime__required | parse_date: "%Y-%m-%d" %}
{% if valid_end_datetime == blank %}
{% unless event.preview %}
{% error "The event end date is not a valid date. Please re-enter it (per the task documentation) and try scheduling the event again." %}
{% break %}
{% endunless %}
{% endif %}
{% endif %}
{% assign now = "now" | date: "%s" %}
{% assign start_datetime_s = options.event_start_datetime__required | date: "%s" %}
{% assign end_datetime_s = options.event_end_datetime__required | date: "%s" %}
{% if start_datetime_s <= now or start_datetime_s >= end_datetime_s %}
{% unless event.preview %}
{% error "The event start and end dates must be future dates and the start date must be before the end date. Please re-enter them (per the task documentation) and try scheduling the event again." %}
{% break %}
{% endunless %}
{% endif %}
{% assign set_compare_at_prices = options.set_compare_at_prices_to_original_price_during_event__boolean %}
{% assign collection_handles_and_discounts = options.collection_handles_and_discounts__keyval %}
{% assign skus_to_include = options.skus_to_include__array %}
{% assign sku_discount = options.sku_discount %}
{% assign exclude_products_tagged_with = options.exclude_products_tagged_with__array %}
{% assign skus_to_exclude = options.skus_to_exclude__array %}
{% comment %}
-- validate discount entry formats: XX% (percentage discount), X.XX (fixed price), or -X.XX (fixed discount)
{% endcomment %}
{% assign discounts = collection_handles_and_discounts | values | default: array %}
{% if skus_to_include != blank and sku_discount == blank %}
{% unless event.preview %}
{% error "A SKU discount must be configured when specific SKUs are included." %}
{% break %}
{% endunless %}
{% endif %}
{% if sku_discount != blank %}
{% assign discounts = discounts | push: sku_discount %}
{% endif %}
{% for discount in discounts %}
{% if discount contains "%" %}
{% assign discount_percentage = discount | times: 1 %}
{% assign discount_string = discount_percentage | append: "%" %}
{% if discount != discount_string %}
{% unless event.preview %}
{% error
message: "Percentage discount entry incorrect. Please update the task configuration (per the task documentation) and try scheduling the event again.",
discount: discount
%}
{% break %}
{% endunless %}
{% endif %}
{% if discount_percentage <= 0 or discount_percentage >= 100 %}
{% unless event.preview %}
{% error
message: "Percentage discount entries should contain an number between 0 and 100. Please update the task configuration (per the task documentation) and try scheduling the event again.",
discount: discount
%}
{% break %}
{% endunless %}
{% endif %}
{% else %}
{% assign discount_absolute = discount | times: 1 | abs %}
{% if discount_absolute == 0 %}
{% unless event.preview %}
{% error
message: "Fixed price and fixed discount entries must contain an absolute number > 0. Please update the task configuration (per the task documentation) and try scheduling the event again.",
discount: discount
%}
{% break %}
{% endunless %}
{% endif %}
{% endif %}
{% endfor %}
{% assign price_change_event = hash %}
{% assign price_change_event["status"] = "scheduled" %}
{% assign price_change_event["start"] = start_datetime_s %}
{% assign price_change_event["end"] = end_datetime_s %}
{% assign price_change_event["set_compare_at_prices"] = set_compare_at_prices %}
{% assign price_change_event["collection_handles_and_discounts"] = collection_handles_and_discounts %}
{% assign price_change_event["skus_to_include"] = skus_to_include %}
{% assign price_change_event["sku_discount"] = sku_discount %}
{% assign price_change_event["skus_to_exclude"] = skus_to_exclude %}
{% assign price_change_event["exclude_products_tagged_with"] = exclude_products_tagged_with %}
{% log
task_options: task.options,
price_change_event: price_change_event
%}
{% assign price_change_event_id = event.id | default: task.id %}
{% assign price_change_events[price_change_event_id] = price_change_event %}
{% action "shopify" %}
mutation {
metafieldsSet(
metafields: [
{
ownerId: {{ shop.id | json }}
namespace: {{ shop_metafield_namespace | json }}
key: {{ shop_metafield_key | json }}
value: {{ price_change_events | json | json }}
type: "json"
}
]
) {
metafields {
id
namespace
key
type
jsonValue
owner {
... on Shop {
id
name
myshopifyDomain
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% comment %}
-- schedule start and end events
{% endcomment %}
{% action "event" %}
{
"topic": "user/price_changes/start",
"task_id": {{ task.id | json }},
"run_at": {{ start_datetime_s | json }},
"data": {
"price_change_event_id": {{ price_change_event_id | json }}
}
}
{% endaction %}
{% action "event" %}
{
"topic": "user/price_changes/end",
"task_id": {{ task.id | json }},
"run_at": {{ end_datetime_s | json }},
"data": {
"price_change_event_id": {{ price_change_event_id | json }}
}
}
{% endaction %}
{% capture email_subject %}New price change event scheduled ({{ price_change_event_id }}){% endcapture %}
{% capture email_body %}
A new price change event has been scheduled, from the {{ task_admin_link }} task within the Mechanic app.
<strong>Price change event ID:</strong> {{ price_change_event_id }}
<strong>Status:</strong> {{ price_change_event["status"] }}
<strong>Event start:</strong> {{ price_change_event["start"] | date: "%F %H:%M %z" }}
<strong>Event end:</strong> {{ price_change_event["end"] | date: "%F %H:%M %z" }}
<strong>Set compare at price to original price during event:</strong> {{ price_change_event["set_compare_at_prices"] }}
<strong>Collection handles and discounts:</strong>
{% for keyval in price_change_event["collection_handles_and_discounts"] -%}
- {{ keyval[0] }}: {{ keyval[1] }}
{% else -%}
n/a
{%- endfor %}
<strong>SKUs to include:</strong> {{ price_change_event["skus_to_include"] | join: ", " | default: "n/a" }}
<strong>SKU discount:</strong> {{ price_change_event["sku_discount"] | default: "n/a" }},
<strong>SKUs to exclude:</strong> {{ price_change_event["skus_to_exclude"] | join: ", " | default: "n/a" }}
<strong>Exclude products tagged with:</strong> {{ price_change_event["exclude_products_tagged_with"] | join: ", " | default: "n/a" }}
<em><strong>Note:</strong> To cancel this event while it is still scheduled or ongoing, use the "cancel" keyword along with the price change event ID when running the task.</em>
{% endcapture %}
{% action "email" %}
{
"to": {{ email_recipients | json }},
"subject": {{ email_subject | json }},
"body": {{ email_body | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% when "list" %}
{% action "echo" price_change_events: price_change_events %}
{% when "email" %}
{% if price_change_events == blank %}
{% assign email_body = "There are currently no configured price change events" %}
{% else %}
{% capture email_body %}
Currently configured price change events, using the {{ task_admin_link }} task within the Mechanic app.
{% for price_change_event in price_change_events -%}
{%- assign price_change_event_id = price_change_event[0] -%}
{%- assign price_change_event_data = price_change_event[1] -%}
<strong>Price change event ID:</strong> {{ price_change_event_id }}
<strong>Status:</strong> {{ price_change_event_data["status"] }}
<strong>Event start:</strong> {{ price_change_event_data["start"] | date: "%F %H:%M %z" }}
<strong>Event end:</strong> {{ price_change_event_data["end"] | date: "%F %H:%M %z" }}
<strong>Set compare at price to original price during event:</strong> {{ price_change_event_data["set_compare_at_prices"] }}
<strong>Collection handles and discounts:</strong>
{% for keyval in price_change_event_data["collection_handles_and_discounts"] -%}
- {{ keyval[0] }}: {{ keyval[1] }}
{% else -%}
n/a
{%- endfor %}
<strong>SKUs to include:</strong> {{ price_change_event_data["skus_to_include"] | join: ", " | default: "n/a" }}
<strong>SKU discount:</strong> {{ price_change_event_data["sku_discount"] | default: "n/a" }},
<strong>SKUs to exclude:</strong> {{ price_change_event_data["skus_to_exclude"] | join: ", " | default: "n/a" }}
<strong>Exclude products tagged with:</strong> {{ price_change_event_data["exclude_products_tagged_with"] | join: ", " | default: "n/a" }}
<hr />
{%- endfor %}
{% endcapture %}
{% endif %}
{% action "email" %}
{
"to": {{ email_recipients | json }},
"subject": "All price change events as of {{ "now" | date: "%F" }}",
"body": {{ email_body | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% when "reset" %}
{% action "event" %}
{
"topic": "user/price_changes/reset",
"task_id": {{ task.id | json }}
}
{% endaction %}
{% else %}
{% if action_keyword contains "cancel" %}
{% assign price_change_event_id = action_keyword | remove: "cancel " %}
{% assign price_change_event = price_change_events[price_change_event_id] %}
{% if price_change_event == blank %}
{% error "Keyword of 'cancel' entered with an invalid price change event ID. Run task with 'list' keyword to see a list of all configured price change events." %}
{% break %}
{% endif %}
{% if price_change_event.status == "scheduled" %}
{% comment %}
-- price change event has not started, so can just update it to cancelled and when the start/end events run they will ignore this event
{% endcomment %}
{% assign price_change_event["status"] = "cancelled" %}
{% assign price_change_events[price_change_event_id] = price_change_event %}
{% action "shopify" %}
mutation {
metafieldsSet(
metafields: [
{
ownerId: {{ shop.id | json }}
namespace: {{ shop_metafield_namespace | json }}
key: {{ shop_metafield_key | json }}
value: {{ price_change_events | json | json }}
type: "json"
}
]
) {
metafields {
id
namespace
key
type
jsonValue
owner {
... on Shop {
id
name
myshopifyDomain
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% capture email_subject %}Scheduled price change event has been cancelled ({{ price_change_event_id }}){% endcapture %}
{% capture email_body %}
A scheduled price change event has been cancelled, from the {{ task_admin_link }} task within the Mechanic app.
<strong>Price change event ID:</strong> {{ price_change_event_id }}
<strong>Status:</strong> {{ price_change_event["status"] }}
<strong>Event start:</strong> {{ price_change_event["start"] | date: "%F %H:%M %z" }}
<strong>Event end:</strong> {{ price_change_event["end"] | date: "%F %H:%M %z" }}
<strong>Set compare at price to original price during event:</strong> {{ price_change_event["set_compare_at_prices"] }}
<strong>Collection handles and discounts:</strong>
{% for keyval in price_change_event["collection_handles_and_discounts"] -%}
- {{ keyval[0] }}: {{ keyval[1] }}
{% else -%}
n/a
{%- endfor %}
<strong>SKUs to include:</strong> {{ price_change_event["skus_to_include"] | join: ", " | default: "n/a" }}
<strong>SKU discount:</strong> {{ price_change_event["sku_discount"] | default: "n/a" }},
<strong>SKUs to exclude:</strong> {{ price_change_event["skus_to_exclude"] | join: ", " | default: "n/a" }}
<strong>Exclude products tagged with:</strong> {{ price_change_event["exclude_products_tagged_with"] | join: ", " | default: "n/a" }}
{% endcapture %}
{% action "email" %}
{
"to": {{ email_recipients | json }},
"subject": {{ email_subject | json }},
"body": {{ email_body | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% elsif price_change_event.status == "ongoing" %}
{% comment %}
-- price change event in progress, so we have to revert the price changes to properly cancel it
{% endcomment %}
{% action "event" %}
{
"topic": "user/price_changes/end",
"task_id": {{ task.id | json }},
"data": {
"price_change_event_id": {{ price_change_event_id | json }},
"cancel": true
}
}
{% endaction %}
{% else %}
{% action "echo"
message: "Price change event cannot be cancelled because it does not have a status of 'scheduled' or 'ongoing'",
price_change_event: price_change_event
%}
{% endif %}
{% else %}
{% error
message: "Unrecognized action keyword. Action keyword must be one of 'schedule', 'list', 'email', or 'cancel'",
action_keyword: action_keyword
%}
{% endif %}
{% endcase %}
{% elsif event.topic == "user/price_changes/start" %}
{% assign price_change_event_id = event.data.price_change_event_id %}
{% assign price_change_event = price_change_events[price_change_event_id] %}
{% if event.preview %}
{% assign price_change_event_id = "01234567-89ab-cdef" %}
{% capture price_change_event_json %}
{
"status": "scheduled",
"start": {{ "now + 1 day" | date: "%s" }},
"end": {{ "now + 2 days" | date: "%s" }},
"set_compare_at_prices": false,
"collection_handles_and_discounts": {
"alpha-beta-gamma": "10%",
"sticks-and-stones": "-5.50",
"lorem-ipsum": "3"
},
"skus_to_include": [
"SKU-123",
"SKU-456"
],
"sku_discount": "15%",
"skus_to_exclude": [
"SKU-987"
],
"exclude_products_tagged_with": [
"do-not-discount"
]
}
{% endcapture %}
{% assign price_change_event = price_change_event_json | parse_json %}
{% endif %}
{% assign status = price_change_event.status %}
{% assign set_compare_at_prices = price_change_event.set_compare_at_prices %}
{% assign collection_handles_and_discounts = price_change_event.collection_handles_and_discounts %}
{% assign skus_to_include = price_change_event.skus_to_include %}
{% assign sku_discount = price_change_event.sku_discount %}
{% assign exclude_products_tagged_with = price_change_event.exclude_products_tagged_with %}
{% assign skus_to_exclude = price_change_event.skus_to_exclude %}
{% if status != "scheduled" %}
{% log
message: "This price change event does not have a status of scheduled, and thus will not start.",
price_change_event: price_change_event
%}
{% break %}
{% endif %}
{% comment %}
-- get collection ids for products query
{% endcomment %}
{% assign in_collection_checks = array %}
{% for keyval in collection_handles_and_discounts %}
{% capture collection_query %}
query {
collectionByHandle(handle: {{ keyval[0] | json }}) {
id
handle
}
}
{% endcapture %}
{% assign collection_result = collection_query | shopify %}
{% assign collection = collection_result.data.collectionByHandle %}
{% if collection != blank %}
{% capture in_collection_check -%}
inCollection_{{ collection.handle | replace: "-", "_" }}: inCollection(id: {{ collection.id | json }})
{%- endcapture %}
{% assign in_collection_checks = in_collection_checks | push: in_collection_check %}
{% endif %}
{% endfor %}
{% assign query_filter = "gift_card:false" %}
{% for tag in exclude_products_tagged_with %}
{% assign query_filter = tag | json | prepend: " tag_not:" | prepend: query_filter %}
{% endfor %}
{% log query_filter: query_filter %}
{% assign products = array %}
{% assign cursor = nil %}
{% for n in (1..200) %}
{% capture products_query %}
query {
products(
first: 250
after: {{ cursor | json }}
query: {{ query_filter | json }}
) {
pageInfo {
hasNextPage
}
edges {
cursor
node {
id
title
tags
variants(first: 100) {
edges {
node {
id
title
sku
price
compareAtPrice
metafield(
namespace: {{ variant_metafield_namespace | json }}
key: {{ variant_metafield_key | json }}
) {
jsonValue
}
}
}
}
{{ in_collection_checks | join: newline }}
}
}
}
}
{% endcapture %}
{% assign products_result = products_query | shopify %}
{% if event.preview %}
{% capture products_result_json %}
{
"data": {
"products": {
"edges": [
{
"node": {
"id": "gid://shopify/Product/1234567890",
"variants": {
"edges": [
{
"node": {
"id": "gid://shopify/ProductVariant/1234567890",
"sku": "ACME-BRICK-RED",
"price": "10.00",
"compareAtPrice": "15.00",
"metafield": null
}
}
]
},
"inCollection_{{ collection_handles_and_discounts.first.first | replace: "-", "_" | default: "sample" }}": true
}
}
]
}
}
}
{% endcapture %}
{% assign products_result = products_result_json | parse_json %}
{% endif %}
{% assign products = products_result.data.products.edges | map: "node" %}
{% for product in products %}
{% comment %}
-- For each product, match to a collection discount if applicable, then, if needed, loop through variants to see if there are any overrides
{% endcomment %}
{% assign product_level_discount = nil %}
{% for keyval in collection_handles_and_discounts %}
{% assign in_collection_label
= "inCollection_"
| append: keyval[0]
| replace: "-", "_"
%}
{% if product[in_collection_label] %}
{% assign product_level_discount = keyval[1] %}
{% break %}
{% endif %}
{% endfor %}
{% if product_level_discount == blank
and skus_to_include == blank
and skus_to_exclude == blank
%}
{% log
message: "This product is not within any configured collections, nor does the price change event have any sku inclusion or exclusion settings; skipping. ",
product: product
%}
{% continue %}
{% endif %}
{% assign variants = product.variants.edges | map: "node" %}
{% assign variant_updates = array %}
{% for variant in variants %}
{% if skus_to_exclude != blank and skus_to_exclude contains variant.sku %}
{% log
message: "This variant was excluded by SKU",
variant: variant
%}
{% continue %}
{% endif %}
{% if variant.metafield != blank %}
{% log
message: "This variant is already part of a price change event; skipping.",
variant: variant
%}
{% continue %}
{% endif %}
{% assign discount_to_apply = product_level_discount %}
{% if skus_to_include != blank and skus_to_include contains variant.sku %}
{% assign discount_to_apply = sku_discount %}
{% endif %}
{% log
product_title: product.title,
sku: variant.sku,
skus_to_include: skus_to_include,
sku_discount: sku_discount,
discount_to_apply: discount_to_apply
%}
{% if discount_to_apply == blank %}
{% log
message: "This variant's product is not within any configured collections, nor has this variant been specifically included by sku; skipping.",
variant: variant
%}
{% continue %}
{% endif %}
{% assign price_to_set = nil %}
{% assign compare_at_price_to_set = nil %}
{% if discount_to_apply contains "%" %}
{% assign price_to_set
= discount_to_apply
| remove: "%"
| minus: 100
| abs
| times: variant.price
| divided_by: 100
| round: 2
%}
{% elsif discount_to_apply contains "-" %}
{% assign price_to_set
= variant.price
| plus: discount_to_apply
| at_least: 0.0
%}
{% else %}
{% assign price_to_set = discount_to_apply %}
{% endif %}
{% if set_compare_at_prices %}
{% assign compare_at_price_to_set = variant.price %}
{% endif %}
{% log
product: product,
product_level_discount: product_level_discount,
variant: variant,
discount_to_apply: discount_to_apply,
price_to_set: price_to_set,
compare_at_price_to_set: compare_at_price_to_set
%}
{% capture variant_metafield_value %}
{
"price_change_event_id": {{ price_change_event_id | json }},
"discount_to_apply": {{ discount_to_apply | json }},
{% if set_compare_at_prices %}
"original_compare_at_price": {{ variant.compareAtPrice | json }},
"compare_at_price_to_set": {{ compare_at_price_to_set | json }},
{% endif %}
"original_price": {{ variant.price | json }},
"price_to_set": {{ price_to_set | json }}
}
{% endcapture %}
{% capture variant_update %}
{
id: {{ variant.id | json}}
price: {{ price_to_set | json }}
{% if set_compare_at_prices %}compareAtPrice: {{ variant.price | json }}{% endif %}
metafields: [
{
key: {{ variant_metafield_key | json }}
namespace: {{ variant_metafield_namespace | json }}
type: "json"
value: {{ variant_metafield_value | json }}
}
]
}
{% endcapture %}
{% assign variant_updates = variant_updates | push: variant_update %}
{% endfor %}
{% if variant_updates != blank %}
{% action "shopify" %}
mutation {
productVariantsBulkUpdate(
productId: {{ product.id | json }}
variants: [
{{ variant_updates | join: newline }}
]
) {
product {
id
title
tags
}
productVariants {
id
title
sku
price
compareAtPrice
metafield(
namespace: {{ variant_metafield_namespace | json }}
key: {{ variant_metafield_key | json }}
) {
jsonValue
}
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}
{% if products_result.data.products.pageInfo.hasNextPage %}
{% assign cursor = products_result.data.products.edges.last.cursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% comment %}
-- update the price change event
{% endcomment %}
{% assign price_change_event["status"] = "ongoing" %}
{% assign price_change_events[price_change_event_id] = price_change_event %}
{% action "shopify" %}
mutation {
metafieldsSet(
metafields: [
{
ownerId: {{ shop.id | json }}
namespace: {{ shop_metafield_namespace | json }}
key: {{ shop_metafield_key | json }}
value: {{ price_change_events | json | json }}
type: "json"
}
]
) {
metafields {
id
namespace
key
type
value
owner {
... on Shop {
id
name
myshopifyDomain
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% capture email_subject %}A scheduled price change event has started ({{ price_change_event_id }}){% endcapture %}
{% capture email_body %}
A scheduled price change event has started, using the {{ task_admin_link }} task within the Mechanic app.
<strong>Price change event ID:</strong> {{ price_change_event_id }}
<strong>Status:</strong> {{ price_change_event["status"] }}
<strong>Event start:</strong> {{ price_change_event["start"] | date: "%F %H:%M %z" }}
<strong>Event end:</strong> {{ price_change_event["end"] | date: "%F %H:%M %z" }}
<strong>Set compare at price to original price during event:</strong> {{ price_change_event["set_compare_at_prices"] }}
<strong>Collection handles and discounts:</strong>
{% for keyval in price_change_event["collection_handles_and_discounts"] -%}
- {{ keyval[0] }}: {{ keyval[1] }}
{% else -%}
n/a
{%- endfor %}
<strong>SKUs to include:</strong> {{ price_change_event["skus_to_include"] | join: ", " | default: "n/a" }}
<strong>SKU discount:</strong> {{ price_change_event["sku_discount"] | default: "n/a" }},
<strong>SKUs to exclude:</strong> {{ price_change_event["skus_to_exclude"] | join: ", " | default: "n/a" }}
<strong>Exclude products tagged with:</strong> {{ price_change_event["exclude_products_tagged_with"] | join: ", " | default: "n/a" }}
<em><strong>Note:</strong> To cancel this event while it is ongoing, use the "cancel" keyword along with the price change event ID when running the task.</em>
{% endcapture %}
{% action "email" %}
{
"to": {{ email_recipients | json }},
"subject": {{ email_subject | json }},
"body": {{ email_body | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% elsif event.topic == "user/price_changes/end" %}
{% assign price_change_event_id = event.data.price_change_event_id %}
{% assign price_change_event = price_change_events[price_change_event_id] %}
{% if event.preview %}
{% assign price_change_event_id = "01234567-89ab-cdef" %}
{% assign price_change_event = hash %}
{% assign price_change_event["status"] = "ongoing" %}
{% endif %}
{% if event.data.cancel %}
{% comment %}
-- go ahead and cancel, as status was already checked to be "scheduled" prior to custom event call
{% endcomment %}
{% assign price_change_event["status"] = "cancelled" %}
{% else %}
{% comment %}
-- since this was a scheduled run, need to make sure the event status is "ongoing" before reverting changes
{% endcomment %}
{% if price_change_event.status != "ongoing" %}
{% log
message: "This price change event does not have a status of 'ongoing', and thus will not be reverted.",
price_change_event: price_change_event
%}
{% break %}
{% endif %}
{% assign price_change_event["status"] = "completed" %}
{% endif %}
{% comment %}
-- To revert the price change event, check every variant in the shop to see if the metafield exists and contains this price change event ID
-- Note: Query from the product level so that productVariantsBulkUpdate can be used, causing only one product update event to fire
{% endcomment %}
{% assign products = array %}
{% assign cursor = nil %}
{% for n in (1..200) %}
{% capture products_query %}
query {
products(
first: 250
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
}
edges {
cursor
node {
id
title
tags
variants(first: 100) {
edges {
node {
id
title
sku
price
compareAtPrice
metafield(
namespace: {{ variant_metafield_namespace | json }}
key: {{ variant_metafield_key | json }}
) {
id
jsonValue
}
}
}
}
}
}
}
}
{% endcapture %}
{% assign products_result = products_query | shopify %}
{% if event.preview %}
{% capture products_result_json %}
{
"data": {
"products": {
"edges": [
{
"node": {
"id": "gid://shopify/Product/1234567890",
"variants": {
"edges": [
{
"node": {
"id": "gid://shopify/ProductVariant/1234567890",
"sku": "ACME-BRICK-RED",
"price": "7.50",
"compareAtPrice": "10.00",
"metafield": {
"id": "gid://shopify/Metafield/9876543210",
"jsonValue": {
"price_change_event_id": "01234567-89ab-cdef",
"discount_to_apply": "25%",
"original_compare_at_price": "15.00",
"compare_at_price_to_set": 7.50,
"original_price": "10.00",
"price_to_set": 7.50
}
}
}
}
]
}
}
}
]
}
}
}
{% endcapture %}
{% assign products_result = products_result_json | parse_json %}
{% endif %}
{% assign products = products_result.data.products.edges | map: "node" %}
{% for product in products %}
{% assign variant_updates = array %}
{% assign metafields_to_delete = array %}
{% assign variants_with_metafield = product.variants.edges | map: "node" | where: "metafield" %}
{% for variant in variants_with_metafield %}
{% assign metafield = variant.metafield.jsonValue %}
{% if metafield.price_change_event_id == price_change_event_id %}
{% comment %}
-- revert the prices on this variant and delete the metafield
{% endcomment %}
{% assign variant_update = hash %}
{% assign variant_update["id"] = variant.id %}
{% assign variant_update["price"] = metafield.original_price %}
{% if metafield.original_compare_at_price %}
{% assign variant_update["compareAtPrice"] = metafield.original_compare_at_price %}
{% endif %}
{% assign variant_updates = variant_updates | push: variant_update %}
{% assign metafield_to_delete = hash %}
{% assign metafield_to_delete["ownerId"] = variant.id %}
{% assign metafield_to_delete["namespace"] = variant_metafield_namespace %}
{% assign metafield_to_delete["key"] = variant_metafield_key %}
{% assign metafields_to_delete = metafields_to_delete | push: metafield_to_delete %}
{% endif %}
{% endfor %}
{% if metafields_to_delete != blank %}
{% action "shopify" %}
mutation {
metafieldsDelete(
metafields: {{ metafields_to_delete | graphql_arguments }}
) {
deletedMetafields {
ownerId
namespace
key
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% if variant_updates != blank %}
{% action "shopify" %}
mutation {
productVariantsBulkUpdate(
productId: {{ product.id | json }}
variants: {{ variant_updates | graphql_arguments }}
) {
product {
id
title
}
productVariants {
id
displayName
sku
price
compareAtPrice
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}
{% if products_result.data.products.pageInfo.hasNextPage %}
{% assign cursor = products_result.data.products.edges.last.cursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% comment %}
-- update the price change event
{% endcomment %}
{% assign price_change_events[price_change_event_id] = price_change_event %}
{% action "shopify" %}
mutation {
metafieldsSet(
metafields: [
{
ownerId: {{ shop.id | json }}
namespace: {{ shop_metafield_namespace | json }}
key: {{ shop_metafield_key | json }}
value: {{ price_change_events | json | json }}
type: "json"
}
]
) {
metafields {
id
namespace
key
type
value
owner {
... on Shop {
id
name
myshopifyDomain
}
}
}
userErrors {
code
field
message
}
}
}
{% endaction %}
{% comment %}
-- send email notification about the price change event status change
{% endcomment %}
{% capture email_subject %}A price change event has been {{ price_change_event["status"] }} ({{ price_change_event_id }}){% endcapture %}
{% capture email_body %}
A price change event has been {{ price_change_event["status"] }}, using the {{ task_admin_link }} task within the Mechanic app.
<strong>Price change event ID:</strong> {{ price_change_event_id }}
<strong>Status:</strong> {{ price_change_event["status"] }}
<strong>Event start:</strong> {{ price_change_event["start"] | date: "%F %H:%M %z" }}
<strong>Event end:</strong> {{ price_change_event["end"] | date: "%F %H:%M %z" }}
<strong>Set compare at price to original price during event:</strong> {{ price_change_event["set_compare_at_prices"] }}
<strong>Collection handles and discounts:</strong>
{% for keyval in price_change_event["collection_handles_and_discounts"] -%}
- {{ keyval[0] }}: {{ keyval[1] }}
{% else -%}
n/a
{%- endfor %}
<strong>SKUs to include:</strong> {{ price_change_event["skus_to_include"] | join: ", " | default: "n/a" }}
<strong>SKU discount:</strong> {{ price_change_event["sku_discount"] | default: "n/a" }},
<strong>SKUs to exclude:</strong> {{ price_change_event["skus_to_exclude"] | join: ", " | default: "n/a" }}
<strong>Exclude products tagged with:</strong> {{ price_change_event["exclude_products_tagged_with"] | join: ", " | default: "n/a" }}
{% endcapture %}
{% action "email" %}
{
"to": {{ email_recipients | json }},
"subject": {{ email_subject | json }},
"body": {{ email_body | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% elsif event.topic == "user/price_changes/reset" %}
{% comment %}
-- To reset price change events, check every variant in the shop to see if the price change event metafield exists
-- Note: Query from the product level so that productVariantsBulkUpdate can be used, causing only one product update event to fire
{% endcomment %}
{% assign products = array %}
{% assign cursor = nil %}
{% for n in (1..200) %}
{% capture products_query %}
query {
products(
first: 250
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
}
edges {
cursor
node {
id
title
tags
variants(first: 100) {
edges {
node {
id
title
sku
price
compareAtPrice
metafield(
namespace: {{ variant_metafield_namespace | json }}
key: {{ variant_metafield_key | json }}
) {
id
jsonValue
}
}
}
}
}
}
}
}
{% endcapture %}
{% assign products_result = products_query | shopify %}
{% if event.preview %}
{% capture products_result_json %}
{
"data": {
"products": {
"edges": [
{
"node": {
"id": "gid://shopify/Product/1234567890",
"variants": {
"edges": [
{
"node": {
"id": "gid://shopify/ProductVariant/1234567890",
"sku": "ACME-BRICK-RED",
"price": "7.50",
"compareAtPrice": "10.00",
"metafield": {
"id": "gid://shopify/Metafield/9876543210",
"jsonValue": {
"price_change_event_id": "01234567-89ab-cdef",
"discount_to_apply": "25%",
"original_compare_at_price": "15.00",
"compare_at_price_to_set": 7.50,
"original_price": "10.00",
"price_to_set": 7.50
}
}
}
}
]
}
}
}
]
}
}
}
{% endcapture %}
{% assign products_result = products_result_json | parse_json %}
{% endif %}
{% assign products = products_result.data.products.edges | map: "node" %}
{% for product in products %}
{% assign variant_updates = array %}
{% assign metafields_to_delete = array %}
{% assign variants_with_metafield = product.variants.edges | map: "node" | where: "metafield" %}
{% log
product_id: product.id,
variants_count: product.variants.edges.size,
variants_with_metafield: variants_with_metafield.size
%}
{% for variant in variants_with_metafield %}
{% assign metafield = variant.metafield.jsonValue %}
{% comment %}
-- revert the prices on this variant and delete the metafield
{% endcomment %}
{% assign variant_update = hash %}
{% assign variant_update["id"] = variant.id %}
{% assign variant_update["price"] = metafield.original_price %}
{% if metafield.original_compare_at_price %}
{% assign variant_update["compareAtPrice"] = metafield.original_compare_at_price %}
{% endif %}
{% assign variant_updates = variant_updates | push: variant_update %}
{% assign metafield_to_delete = hash %}
{% assign metafield_to_delete["ownerId"] = variant.id %}
{% assign metafield_to_delete["namespace"] = variant_metafield_namespace %}
{% assign metafield_to_delete["key"] = variant_metafield_key %}
{% assign metafields_to_delete = metafields_to_delete | push: metafield_to_delete %}
{% endfor %}
{% if metafields_to_delete != blank %}
{% action "shopify" %}
mutation {
metafieldsDelete(
metafields: {{ metafields_to_delete | graphql_arguments }}
) {
deletedMetafields {
ownerId
namespace
key
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% if variant_updates != blank %}
{% action "shopify" %}
mutation {
productVariantsBulkUpdate(
productId: {{ product.id | json }}
variants: {{ variant_updates | graphql_arguments }}
) {
product {
id
title
}
productVariants {
id
displayName
sku
price
compareAtPrice
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}
{% if products_result.data.products.pageInfo.hasNextPage %}
{% assign cursor = products_result.data.products.edges.last.cursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}
{% comment %}
-- delete the price change events shop metafield
{% endcomment %}
{% action "shopify" %}
mutation {
metafieldsDelete(
metafields: [
{
ownerId: {{ shop.id | json }}
namespace: {{ shop_metafield_namespace | json }}
key: {{ shop_metafield_key | json }}
}
]
) {
deletedMetafields {
ownerId
namespace
key
}
userErrors {
field
message
}
}
}
{% endaction %}
{% comment %}
-- send email notification about the price change events reset
{% endcomment %}
{% capture email_subject %}All price change events have been cleared{% endcapture %}
{% capture email_body %}
All price change events have been reverted and cleared, using the {{ task_admin_link }} task within the Mechanic app.
{% endcapture %}
{% action "email" %}
{
"to": {{ email_recipients | json }},
"subject": {{ email_subject | json }},
"body": {{ email_body | newline_to_br | json }},
"reply_to": {{ shop.customer_email | json }},
"from_display_name": {{ shop.name | json }}
}
{% endaction %}
{% endif %}
Mechanic Liquid reference
Default option values
- Set compare at prices to original price during event
false