Skip to content

Commit

Permalink
switch to bulk mutation due to deprecation (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
tekhaus authored Aug 27, 2024
1 parent cec2a32 commit a47e252
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```

Expand All @@ -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
```

Expand All @@ -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

Expand Down
318 changes: 148 additions & 170 deletions docs/auto-price-products-based-on-their-compare-at-prices/script.liquid
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 %}
Loading

0 comments on commit a47e252

Please sign in to comment.