-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-price-products-based-on-their-compare-at-prices.json
25 lines (25 loc) · 7.3 KB
/
auto-price-products-based-on-their-compare-at-prices.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
"docs": "Use this task when the pricing for your products can be automatically calculated by multiplying with compare-at prices.\n\nRun this task manually to process your entire product catalog, optionally filtering by product tag. Or, choose to have this task run whenever products are updated, or run daily, at midnight in your store's local timezone.",
"halt_action_run_sequence_on_error": false,
"name": "Auto-price products based on their compare-at prices",
"online_store_javascript": null,
"options": {
"multiplier_when_calculating_price__number_required": 0.8,
"required_product_tag": null,
"test_mode__boolean": true,
"run_automatically_for_product_updates__boolean": false,
"run_daily__boolean": null
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"script": "{% comment %}\n Preferred option order:\n\n {{ options.multiplier_when_calculating_price__number_required }}\n {{ options.required_product_tag }}\n {{ options.run_automatically_for_product_updates__boolean }}\n {{ options.run_daily__boolean }}\n {{ options.test_mode__boolean }}\n{% endcomment %}\n\n{% if options.multiplier_when_calculating_price__number_required < 0 %}\n {% error \"Price multiplier must be at least 0. :)\" %}\n{% endif %}\n\n{% if event.topic contains \"shopify/products/\" %}\n {% if event.preview %}\n {% capture product_json %}\n {\n \"tags\": {{ options.required_product_tag | json }},\n \"variants\": [\n {\n \"id\": 1234567890,\n \"admin_graphql_api_id\": \"gid://shopify/ProductVariant/1234567890\",\n \"sku\": \"ABC123\",\n \"price\": \"999.00\",\n \"compare_at_price\": \"10.00\"\n }\n ]\n }\n {% endcapture %}\n\n {% assign product = product_json | parse_json %}\n {% endif %}\n\n {% assign product_tags = product.tags | split: \", \" %}\n {% if options.required_product_tag == blank or product_tags contains options.required_product_tag %}\n {% assign summaries = array %}\n {% assign mutations = array %}\n\n {% for variant in product.variants %}\n {% if variant.compare_at_price == blank %}\n {% continue %}\n {% endif %}\n\n {% assign compare_at_price = variant.compare_at_price | times: 1 %}\n {% assign price = variant.price | times: 1 %}\n {% assign desired_price = compare_at_price | times: options.multiplier_when_calculating_price__number_required %}\n\n {% if desired_price != price %}\n {% if options.test_mode__boolean %}\n {% assign summary = hash %}\n {% assign summary[\"id\"] = variant.id %}\n {% assign summary[\"sku\"] = variant.sku %}\n {% assign summary[\"compare_at_price\"] = compare_at_price %}\n {% assign summary[\"current_price\"] = price %}\n {% assign summary[\"desired_price\"] = desired_price %}\n {% assign summaries[summaries.size] = summary %}\n {% else %}\n {% capture mutation %}\n productVariantUpdate{{ forloop.index }}: productVariantUpdate(\n input: {\n id: {{ variant.admin_graphql_api_id | json }}\n price: {{ desired_price | append: \"\" | json }}\n }\n ) {\n productVariant {\n price\n compareAtPrice\n }\n userErrors {\n field\n message\n }\n }\n {% endcapture %}\n\n {% assign mutations[mutations.size] = mutation %}\n {% endif %}\n {% endif %}\n {% endfor %}\n\n {% if options.test_mode__boolean %}\n {% if summaries != empty %}\n {% action \"echo\" summaries %}\n {% endif %}\n {% elsif mutations != empty %}\n {% action \"shopify\" %}\n mutation {\n {{ mutations | join: newline }}\n }\n {% endaction %}\n {% endif %}\n {% endif %}\n{% elsif event.topic == \"mechanic/user/trigger\" or event.topic contains \"mechanic/scheduler/\" %}\n {% assign cursor = nil %}\n\n {% assign productVariants_query = nil %}\n {% if options.required_product_tag != blank %}\n {% assign productVariants_query = options.required_product_tag | json | prepend: \"tag:\" %}\n {% endif %}\n\n {% for n in (0..100) %}\n {% capture query %}\n query {\n productVariants(\n first: 250\n after: {{ cursor | json }}\n query: {{ productVariants_query | json }}\n ) {\n pageInfo {\n hasNextPage\n }\n edges {\n cursor\n node {\n id\n displayName\n price\n compareAtPrice\n }\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"productVariants\": {\n \"edges\": [\n {\n \"node\": {\n \"id\": \"gid://shopify/ProductVariant/1234567890\",\n \"displayName\": \"IPod Nano - 8GB\",\n \"price\": \"999.00\",\n \"compareAtPrice\": \"10.00\"\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% for productVariant_edge in result.data.productVariants.edges %}\n {% assign productVariant = productVariant_edge.node %}\n\n {% if productVariant.compareAtPrice == blank %}\n {% continue %}\n {% endif %}\n\n {% assign compare_at_price = productVariant.compareAtPrice | times: 1 %}\n {% assign price = productVariant.price | times: 1 %}\n {% assign desired_price = compare_at_price | times: options.multiplier_when_calculating_price__number_required %}\n\n {% if desired_price != price %}\n {% if options.test_mode__boolean %}\n {% action \"echo\" name: productVariant.displayName, compare_at_price: compare_at_price, current_price: price, desired_price: desired_price %}\n {% else %}\n {% action \"shopify\" %}\n mutation {\n productVariantUpdate(\n input: {\n id: {{ productVariant.id | json }}\n price: {{ desired_price | append: \"\" | json }}\n }\n ) {\n productVariant {\n price\n compareAtPrice\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% endif %}\n {% endfor %}\n\n {% if result.data.productVariants.pageInfo.hasNextPage %}\n {% assign cursor = result.data.productVariants.edges.last.cursor %}\n {% else %}\n {% break %}\n {% endif %}\n {% endfor %}\n{% endif %}",
"subscriptions": [
"mechanic/user/trigger"
],
"subscriptions_template": "{% if options.run_automatically_for_product_updates__boolean %}\n shopify/products/update\n{% endif %}\n\n{% if options.run_daily__boolean %}\n mechanic/scheduler/daily\n{% endif %}\n\nmechanic/user/trigger",
"tags": [
"Discounts",
"Price",
"Products"
]
}