From e3105b4584e57001a00af478834f91b3f7efd01d Mon Sep 17 00:00:00 2001 From: Brad Hover Date: Tue, 27 Aug 2024 14:36:20 -0700 Subject: [PATCH] switch to bulk mutation due to deprecation --- .../README.md | 11 +- .../script.liquid | 318 ++++++++---------- ...ucts-based-on-their-compare-at-prices.json | 13 +- 3 files changed, 160 insertions(+), 182 deletions(-) diff --git a/docs/auto-price-products-based-on-their-compare-at-prices/README.md b/docs/auto-price-products-based-on-their-compare-at-prices/README.md index 2710665d..02fd6c82 100644 --- a/docs/auto-price-products-based-on-their-compare-at-prices/README.md +++ b/docs/auto-price-products-based-on-their-compare-at-prices/README.md @@ -13,10 +13,11 @@ Use this task when the pricing for your products can be automatically calculated ```json { "multiplier_when_calculating_price__number_required": 0.8, - "required_product_tag": null, - "test_mode__boolean": true, + "product_tag_to_monitor": null, + "only_process_active_products__boolean": true, "run_automatically_for_product_updates__boolean": false, - "run_daily__boolean": null + "run_daily__boolean": null, + "test_mode__boolean": true } ``` @@ -28,11 +29,9 @@ Use this task when the pricing for your products can be automatically calculated {% if options.run_automatically_for_product_updates__boolean %} shopify/products/update {% endif %} - {% if options.run_daily__boolean %} mechanic/scheduler/daily {% endif %} - mechanic/user/trigger ``` @@ -42,7 +41,7 @@ mechanic/user/trigger Use this task when the pricing for your products can be automatically calculated by multiplying with compare-at prices. -Run 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. +Run this task manually to process your entire product catalog, optionally filtering by active products or product tag. You may also choose to have this task run whenever products are updated, or run daily, at midnight in your store's local timezone. ## Installing this task diff --git a/docs/auto-price-products-based-on-their-compare-at-prices/script.liquid b/docs/auto-price-products-based-on-their-compare-at-prices/script.liquid index 162b03dd..6d2cabf3 100644 --- a/docs/auto-price-products-based-on-their-compare-at-prices/script.liquid +++ b/docs/auto-price-products-based-on-their-compare-at-prices/script.liquid @@ -1,195 +1,173 @@ -{% comment %} - Preferred option order: - - {{ options.multiplier_when_calculating_price__number_required }} - {{ options.required_product_tag }} - {{ options.run_automatically_for_product_updates__boolean }} - {{ options.run_daily__boolean }} - {{ options.test_mode__boolean }} -{% endcomment %} - -{% if options.multiplier_when_calculating_price__number_required < 0 %} +{% assign multiplier_when_calculating_price = options.multiplier_when_calculating_price__number_required %} +{% assign product_tag_to_monitor = options.product_tag_to_monitor %} +{% assign only_process_active_products = options.only_process_active_products__boolean %} +{% assign run_automatically_for_product_updates = options.run_automatically_for_product_updates__boolean %} +{% assign run_daily = options.run_daily__boolean %} +{% assign test_mode = options.test_mode__boolean %} + +{% if multiplier_when_calculating_price < 0 %} {% error "Price multiplier must be at least 0. :)" %} {% endif %} -{% if event.topic contains "shopify/products/" %} - {% if event.preview %} - {% capture product_json %} - { - "tags": {{ options.required_product_tag | json }}, - "variants": [ - { - "id": 1234567890, - "admin_graphql_api_id": "gid://shopify/ProductVariant/1234567890", - "sku": "ABC123", - "price": "999.00", - "compare_at_price": "10.00" - } - ] - } - {% endcapture %} - - {% assign product = product_json | parse_json %} - {% endif %} +{% assign search_query = nil %} - {% assign product_tags = product.tags | split: ", " %} - {% if options.required_product_tag == blank or product_tags contains options.required_product_tag %} - {% assign summaries = array %} - {% assign mutations = array %} +{% if only_process_active_products %} + {% assign search_query = "product_status:active" %} +{% endif %} - {% for variant in product.variants %} - {% if variant.compare_at_price == blank %} - {% continue %} - {% endif %} +{% if product_tag_to_monitor != blank %} + {%- capture search_query -%} + {{ search_query }} tag:{{ product_tag_to_monitor | json }} + {%- endcapture -%} +{% endif %} - {% assign compare_at_price = variant.compare_at_price | times: 1 %} - {% assign price = variant.price | times: 1 %} - {% assign desired_price = compare_at_price | times: options.multiplier_when_calculating_price__number_required %} - - {% if desired_price != price %} - {% if options.test_mode__boolean %} - {% assign summary = hash %} - {% assign summary["id"] = variant.id %} - {% assign summary["sku"] = variant.sku %} - {% assign summary["compare_at_price"] = compare_at_price %} - {% assign summary["current_price"] = price %} - {% assign summary["desired_price"] = desired_price %} - {% assign summaries[summaries.size] = summary %} - {% else %} - {% capture mutation %} - productVariantUpdate{{ forloop.index }}: productVariantUpdate( - input: { - id: {{ variant.admin_graphql_api_id | json }} - price: {{ desired_price | append: "" | json }} - } - ) { - productVariant { - price - compareAtPrice - } - userErrors { - field - message - } - } - {% endcapture %} +{% comment %} + -- for product create/update filter the variants query with the product ID that caused the event +{% endcomment %} - {% assign mutations[mutations.size] = mutation %} - {% endif %} - {% endif %} - {% endfor %} +{% if event.topic == "shopify/products/update" %} + {%- capture search_query -%} + product_id:{{ product.id }} {{ search_query }} + {%- endcapture -%} +{% endif %} - {% if options.test_mode__boolean %} - {% if summaries != empty %} - {% action "echo" summaries %} - {% endif %} - {% elsif mutations != empty %} - {% action "shopify" %} - mutation { - {{ mutations | join: newline }} - } - {% endaction %} - {% endif %} - {% endif %} -{% elsif event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %} - {% assign cursor = nil %} +{% unless event.preview %} + {% log search_query: search_query %} +{% endunless %} - {% assign productVariants_query = nil %} - {% if options.required_product_tag != blank %} - {% assign productVariants_query = options.required_product_tag | json | prepend: "tag:" %} - {% endif %} +{% comment %} + -- querying variants resource to support 2K variants per product without two concurrent pagination loops +{% endcomment %} - {% for n in (0..100) %} - {% capture query %} - query { - productVariants( - first: 250 - after: {{ cursor | json }} - query: {{ productVariants_query | json }} - ) { - pageInfo { - hasNextPage - } - edges { - cursor - node { - id - displayName - price - compareAtPrice - } +{% assign summaries = array %} +{% assign variant_inputs_by_product = hash %} + +{% assign cursor = nil %} + +{% for n in (0..200) %} + {% capture query %} + query { + productVariants( + first: 250 + after: {{ cursor | json }} + query: {{ search_query | json }} + ) { + pageInfo { + hasNextPage + endCursor + } + nodes { + id + displayName + sku + price + compareAtPrice + product { + id } } } - {% endcapture %} + } + {% endcapture %} + + {% assign result = query | shopify %} - {% assign result = query | shopify %} - - {% if event.preview %} - {% capture result_json %} - { - "data": { - "productVariants": { - "edges": [ - { - "node": { - "id": "gid://shopify/ProductVariant/1234567890", - "displayName": "IPod Nano - 8GB", - "price": "999.00", - "compareAtPrice": "10.00" - } + {% if event.preview %} + {% capture result_json %} + { + "data": { + "productVariants": { + "nodes": [ + { + "id": "gid://shopify/ProductVariant/1234567890", + "displayName": "Widget", + "sku": "WDGT", + "price": "99.00", + "compareAtPrice": "10.00", + "product": { + "id": "gid://shopify/Product/1234567890" } - ] - } + } + ] } } - {% endcapture %} + } + {% endcapture %} - {% assign result = result_json | parse_json %} - {% endif %} + {% assign result = result_json | parse_json %} + {% endif %} - {% for productVariant_edge in result.data.productVariants.edges %} - {% assign productVariant = productVariant_edge.node %} + {% comment %} + -- process this batch of variants to see which qualify to have their price updated + {% endcomment %} - {% if productVariant.compareAtPrice == blank %} - {% continue %} - {% endif %} + {% for variant in result.data.productVariants.nodes %} + {% if variant.compareAtPrice == blank %} + {% continue %} + {% endif %} - {% assign compare_at_price = productVariant.compareAtPrice | times: 1 %} - {% assign price = productVariant.price | times: 1 %} - {% assign desired_price = compare_at_price | times: options.multiplier_when_calculating_price__number_required %} - - {% if desired_price != price %} - {% if options.test_mode__boolean %} - {% action "echo" name: productVariant.displayName, compare_at_price: compare_at_price, current_price: price, desired_price: desired_price %} - {% else %} - {% action "shopify" %} - mutation { - productVariantUpdate( - input: { - id: {{ productVariant.id | json }} - price: {{ desired_price | append: "" | json }} - } - ) { - productVariant { - price - compareAtPrice - } - userErrors { - field - message - } - } - } - {% endaction %} - {% endif %} + {% assign compare_at_price = variant.compareAtPrice | times: 1 %} + {% assign price = variant.price | times: 1 %} + {% assign desired_price + = compare_at_price + | times: multiplier_when_calculating_price + | round: 2 + %} + + {% if desired_price != price %} + {% if test_mode %} + {% assign summary = hash %} + {% assign summary["id"] = variant.id %} + {% assign summary["displayName"] = variant.displayName | remove: " - Default Title" %} + {% assign summary["sku"] = variant.sku %} + {% assign summary["compare_at_price"] = compare_at_price %} + {% assign summary["current_price"] = price %} + {% assign summary["desired_price"] = desired_price %} + {% assign summaries = summaries | push: summary %} + + {% else %} + {% assign variant_input = hash %} + {% assign variant_input["id"] = variant.id %} + {% assign variant_input["price"] = desired_price | append: "" %} + {% assign variant_inputs_by_product[variant.product.id] + = variant_inputs_by_product[variant.product.id] + | default: array + | push: variant_input + %} {% endif %} - {% endfor %} - - {% if result.data.productVariants.pageInfo.hasNextPage %} - {% assign cursor = result.data.productVariants.edges.last.cursor %} - {% else %} - {% break %} {% endif %} {% endfor %} + + {% if result.data.productVariants.pageInfo.hasNextPage %} + {% assign cursor = result.data.productVariants.pageInfo.endCursor %} + {% else %} + {% break %} + {% endif %} +{% endfor %} + +{% for keyval in variant_inputs_by_product %} + {% action "shopify" %} + mutation { + productVariantsBulkUpdate( + productId: {{ keyval[0] | json }} + variants: {{ keyval[1] | graphql_arguments }} + ) { + userErrors { + code + field + message + } + } + } + {% endaction %} + +{% else %} + {% log "No variants qualified to be updated on this task run." %} +{% endfor %} + +{% if summaries != blank %} + {% log + test_mode: test_mode, + variants_to_be_updated: summaries + %} {% endif %} diff --git a/tasks/auto-price-products-based-on-their-compare-at-prices.json b/tasks/auto-price-products-based-on-their-compare-at-prices.json index 2154ce23..19823345 100644 --- a/tasks/auto-price-products-based-on-their-compare-at-prices.json +++ b/tasks/auto-price-products-based-on-their-compare-at-prices.json @@ -1,22 +1,23 @@ { - "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.", + "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 active products or product tag. You may also 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, + "product_tag_to_monitor": null, + "only_process_active_products__boolean": true, "run_automatically_for_product_updates__boolean": false, - "run_daily__boolean": null + "run_daily__boolean": null, + "test_mode__boolean": true }, "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 %}", + "script": "{% assign multiplier_when_calculating_price = options.multiplier_when_calculating_price__number_required %}\n{% assign product_tag_to_monitor = options.product_tag_to_monitor %}\n{% assign only_process_active_products = options.only_process_active_products__boolean %}\n{% assign run_automatically_for_product_updates = options.run_automatically_for_product_updates__boolean %}\n{% assign run_daily = options.run_daily__boolean %}\n{% assign test_mode = options.test_mode__boolean %}\n\n{% if multiplier_when_calculating_price < 0 %}\n {% error \"Price multiplier must be at least 0. :)\" %}\n{% endif %}\n\n{% assign search_query = nil %}\n\n{% if only_process_active_products %}\n {% assign search_query = \"product_status:active\" %}\n{% endif %}\n\n{% if product_tag_to_monitor != blank %}\n {%- capture search_query -%}\n {{ search_query }} tag:{{ product_tag_to_monitor | json }}\n {%- endcapture -%}\n{% endif %}\n\n{% comment %}\n -- for product create/update filter the variants query with the product ID that caused the event\n{% endcomment %}\n\n{% if event.topic == \"shopify/products/update\" %}\n {%- capture search_query -%}\n product_id:{{ product.id }} {{ search_query }}\n {%- endcapture -%}\n{% endif %}\n\n{% unless event.preview %}\n {% log search_query: search_query %}\n{% endunless %}\n\n{% comment %}\n -- querying variants resource to support 2K variants per product without two concurrent pagination loops\n{% endcomment %}\n\n{% assign summaries = array %}\n{% assign variant_inputs_by_product = hash %}\n\n{% assign cursor = nil %}\n\n{% for n in (0..200) %}\n {% capture query %}\n query {\n productVariants(\n first: 250\n after: {{ cursor | json }}\n query: {{ search_query | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n nodes {\n id\n displayName\n sku\n price\n compareAtPrice\n product {\n id\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 \"nodes\": [\n {\n \"id\": \"gid://shopify/ProductVariant/1234567890\",\n \"displayName\": \"Widget\",\n \"sku\": \"WDGT\",\n \"price\": \"99.00\",\n \"compareAtPrice\": \"10.00\",\n \"product\": {\n \"id\": \"gid://shopify/Product/1234567890\"\n }\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% comment %}\n -- process this batch of variants to see which qualify to have their price updated\n {% endcomment %}\n\n {% for variant in result.data.productVariants.nodes %}\n {% if variant.compareAtPrice == blank %}\n {% continue %}\n {% endif %}\n\n {% assign compare_at_price = variant.compareAtPrice | times: 1 %}\n {% assign price = variant.price | times: 1 %}\n {% assign desired_price\n = compare_at_price\n | times: multiplier_when_calculating_price\n | round: 2\n %}\n\n {% if desired_price != price %}\n {% if test_mode %}\n {% assign summary = hash %}\n {% assign summary[\"id\"] = variant.id %}\n {% assign summary[\"displayName\"] = variant.displayName | remove: \" - Default Title\" %}\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 | push: summary %}\n\n {% else %}\n {% assign variant_input = hash %}\n {% assign variant_input[\"id\"] = variant.id %}\n {% assign variant_input[\"price\"] = desired_price | append: \"\" %}\n {% assign variant_inputs_by_product[variant.product.id]\n = variant_inputs_by_product[variant.product.id]\n | default: array\n | push: variant_input\n %}\n {% endif %}\n {% endif %}\n {% endfor %}\n\n {% if result.data.productVariants.pageInfo.hasNextPage %}\n {% assign cursor = result.data.productVariants.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n{% endfor %}\n\n{% for keyval in variant_inputs_by_product %}\n {% action \"shopify\" %}\n mutation {\n productVariantsBulkUpdate(\n productId: {{ keyval[0] | json }}\n variants: {{ keyval[1] | graphql_arguments }}\n ) {\n userErrors {\n code\n field\n message\n }\n }\n }\n {% endaction %}\n\n{% else %}\n {% log \"No variants qualified to be updated on this task run.\" %}\n{% endfor %}\n\n{% if summaries != blank %}\n {% log\n test_mode: test_mode,\n variants_to_be_updated: summaries\n %}\n{% endif %}\n", "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", + "subscriptions_template": "{% if options.run_automatically_for_product_updates__boolean %}\n shopify/products/update\n{% endif %}\n{% if options.run_daily__boolean %}\n mechanic/scheduler/daily\n{% endif %}\nmechanic/user/trigger", "tags": [ "Discounts", "Price",