-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switch to bulk mutation due to deprecation (#392)
- Loading branch information
Showing
3 changed files
with
160 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
318 changes: 148 additions & 170 deletions
318
docs/auto-price-products-based-on-their-compare-at-prices/script.liquid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %} |
Oops, something went wrong.