Skip to content

Commit

Permalink
fix preview that was using REST field (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
tekhaus authored Aug 2, 2024
1 parent 5e21d24 commit 7677e03
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 40 deletions.
12 changes: 7 additions & 5 deletions docs/set-product-templates-based-on-product-tags/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ shopify/products/update

Use this task to automatically select a specific product template for each of your products, based on how each product is tagged. Useful for a set-it-and-forget-it setup. :)

To use this task, fill in "Product tags and template suffixes" with a single product tag on the left, per line, and a product template suffix on the right. For example, if you'd like products tagged "sale" to be shown with the products.sale-hero.liquid template, add an item that has "sale" on the left and "sale-hero" on the right.

If a product qualifies for several template suffixes, based on its tags and on the task configuration, this task will go with whichever tmeplate suffix match is found first.

To apply this task to your entire catalog, use the "Run task" button. Otherwise, this task will run automatically, whenever a product is created or updated.
To use this task, fill in "Product tags and template suffixes" with a single product tag on the left, per line, and a product template suffix on the right. For example, if you'd like products tagged "sale" to be shown with the ```products.sale-hero.liquid``` template, add an item that has "sale" on the left and "sale-hero" on the right.

If a product qualifies for several template suffixes based on its tags and on the task configuration, then this task will go with whichever template suffix match is found first.

To apply this task to your entire catalog, use the "Run task" button. Otherwise, this task will run automatically whenever a product is created or updated.

**Note:** If you want to reset a product to the default product template, then leave the template suffix empty for the associated tag.

## Installing this task

Expand Down
53 changes: 23 additions & 30 deletions docs/set-product-templates-based-on-product-tags/script.liquid
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{% assign product_tags_and_template_suffixes = options.product_tags_and_template_suffixes__keyval_required %}

{% assign products = array %}

{% if event.topic == "mechanic/user/trigger" %}
Expand Down Expand Up @@ -34,7 +36,6 @@
{% endaction %}

{% elsif event.topic contains "shopify/products/" or event.preview %}

{% capture query %}
query {
product(id: {{ product.admin_graphql_api_id | json }} ) {
Expand All @@ -47,33 +48,30 @@

{% assign result = query | shopify %}

{% if event.preview %}
{% capture result_json %}
{
"data": {
"product": {
"id": "gid://shopify/Product/4354268561469",
"tags": ["123", {{ options.product_tags_and_template_suffixes__keyval_required.first.first | json }}],
"template_suffix": {{ options.product_tags_and_template_suffixes__keyval_required.first.last | json }}
}
}
}
{% endcapture %}

{% assign result = result_json | parse_json %}
{% endif %}

{% assign products[0] = result.data.product %}

{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
{% assign products = bulkOperation.objects %}
{% endif %}

{% if event.preview %}
{% capture products_json %}
[
{
"id": "gid://shopify/Product/4354268561469",
"tags": ["123", {{ product_tags_and_template_suffixes.first.first | json }}],
"templateSuffix": "preview-1234567890"
}
]
{% endcapture %}

{% assign products = products_json | parse_json %}
{% endif %}

{% for product in products %}
{% assign product_qualifies_for_update = false %}
{% assign product_template_suffix_to_apply = nil %}

{% for keyval in options.product_tags_and_template_suffixes__keyval_required %}
{% for keyval in product_tags_and_template_suffixes %}
{% assign product_tag_to_check = keyval[0] %}
{% assign product_template_to_apply = keyval[1] %}

Expand All @@ -83,15 +81,7 @@
{% endif %}
{% endfor %}

{% comment %} Allow setting to template to blank{% endcomment %}
{% if product_template_suffix_to_apply == blank and product.templateSuffix != blank %}
{% assign product_qualifies_for_update = true %}
{% comment %} Make sure the template is not the right one already {% endcomment %}
{% elsif product_template_suffix_to_apply != product.templateSuffix %}
{% assign product_qualifies_for_update = true %}
{% endif %}

{% if product_qualifies_for_update %}
{% if product_template_suffix_to_apply != product.templateSuffix %}
{% action "shopify" %}
mutation {
productUpdate(
Expand All @@ -100,13 +90,16 @@
templateSuffix: {{ product_template_suffix_to_apply | json }}
}
) {
product {
id
templateSuffix
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endaction %}
{% endif %}

{% endfor %}
10 changes: 5 additions & 5 deletions tasks/set-product-templates-based-on-product-tags.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"docs": "Use this task to automatically select a specific product template for each of your products, based on how each product is tagged. Useful for a set-it-and-forget-it setup. :)\n\nTo use this task, fill in \"Product tags and template suffixes\" with a single product tag on the left, per line, and a product template suffix on the right. For example, if you'd like products tagged \"sale\" to be shown with the products.sale-hero.liquid template, add an item that has \"sale\" on the left and \"sale-hero\" on the right.\r\n\r\nIf a product qualifies for several template suffixes, based on its tags and on the task configuration, this task will go with whichever tmeplate suffix match is found first.\r\n\r\nTo apply this task to your entire catalog, use the \"Run task\" button. Otherwise, this task will run automatically, whenever a product is created or updated.",
"docs": "Use this task to automatically select a specific product template for each of your products, based on how each product is tagged. Useful for a set-it-and-forget-it setup. :)\n\nTo use this task, fill in \"Product tags and template suffixes\" with a single product tag on the left, per line, and a product template suffix on the right. For example, if you'd like products tagged \"sale\" to be shown with the ```products.sale-hero.liquid``` template, add an item that has \"sale\" on the left and \"sale-hero\" on the right.\n\nIf a product qualifies for several template suffixes based on its tags and on the task configuration, then this task will go with whichever template suffix match is found first.\n\nTo apply this task to your entire catalog, use the \"Run task\" button. Otherwise, this task will run automatically whenever a product is created or updated.\n\n**Note:** If you want to reset a product to the default product template, then leave the template suffix empty for the associated tag.",
"halt_action_run_sequence_on_error": false,
"name": "Set product templates based on product tags",
"online_store_javascript": null,
Expand All @@ -9,12 +9,12 @@
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [],
"script": "{% assign products = array %}\n\n{% if event.topic == \"mechanic/user/trigger\" %}\n {% capture bulk_operation_query %}\n query {\n products {\n edges {\n node {\n __typename\n id\n tags\n templateSuffix\n }\n }\n }\n }\n {% endcapture %}\n\n {% action \"shopify\" %}\n mutation {\n bulkOperationRunQuery(\n query: {{ bulk_operation_query | json }}\n ) {\n bulkOperation {\n id\n status\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n\n{% elsif event.topic contains \"shopify/products/\" or event.preview %}\n\n {% capture query %}\n query {\n product(id: {{ product.admin_graphql_api_id | json }} ) {\n id\n tags\n templateSuffix\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"product\": {\n \"id\": \"gid://shopify/Product/4354268561469\",\n \"tags\": [\"123\", {{ options.product_tags_and_template_suffixes__keyval_required.first.first | json }}],\n \"template_suffix\": {{ options.product_tags_and_template_suffixes__keyval_required.first.last | json }}\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %} \n\n {% assign products[0] = result.data.product %}\n\n{% elsif event.topic == \"mechanic/shopify/bulk_operation\" %}\n {% assign products = bulkOperation.objects %}\n{% endif %}\n\n{% for product in products %}\n {% assign product_qualifies_for_update = false %}\n {% assign product_template_suffix_to_apply = nil %}\n\n {% for keyval in options.product_tags_and_template_suffixes__keyval_required %}\n {% assign product_tag_to_check = keyval[0] %}\n {% assign product_template_to_apply = keyval[1] %}\n\n {% if product.tags contains product_tag_to_check %}\n {% assign product_template_suffix_to_apply = product_template_to_apply %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% comment %} Allow setting to template to blank{% endcomment %}\n {% if product_template_suffix_to_apply == blank and product.templateSuffix != blank %}\n {% assign product_qualifies_for_update = true %}\n {% comment %} Make sure the template is not the right one already {% endcomment %} \n {% elsif product_template_suffix_to_apply != product.templateSuffix %}\n {% assign product_qualifies_for_update = true %}\n {% endif %} \n\n {% if product_qualifies_for_update %}\n {% action \"shopify\" %}\n mutation {\n productUpdate(\n input: {\n id: {{ product.id | json }}\n templateSuffix: {{ product_template_suffix_to_apply | json }}\n }\n ) {\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %} \n {% endif %}\n\n{% endfor %}\n",
"script": "{% assign product_tags_and_template_suffixes = options.product_tags_and_template_suffixes__keyval_required %}\n\n{% assign products = array %}\n\n{% if event.topic == \"mechanic/user/trigger\" %}\n {% capture bulk_operation_query %}\n query {\n products {\n edges {\n node {\n __typename\n id\n tags\n templateSuffix\n }\n }\n }\n }\n {% endcapture %}\n\n {% action \"shopify\" %}\n mutation {\n bulkOperationRunQuery(\n query: {{ bulk_operation_query | json }}\n ) {\n bulkOperation {\n id\n status\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n\n{% elsif event.topic contains \"shopify/products/\" or event.preview %}\n {% capture query %}\n query {\n product(id: {{ product.admin_graphql_api_id | json }} ) {\n id\n tags\n templateSuffix\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% assign products[0] = result.data.product %}\n\n{% elsif event.topic == \"mechanic/shopify/bulk_operation\" %}\n {% assign products = bulkOperation.objects %}\n{% endif %}\n\n{% if event.preview %}\n {% capture products_json %}\n [\n {\n \"id\": \"gid://shopify/Product/4354268561469\",\n \"tags\": [\"123\", {{ product_tags_and_template_suffixes.first.first | json }}],\n \"templateSuffix\": \"preview-1234567890\"\n }\n ]\n {% endcapture %}\n\n {% assign products = products_json | parse_json %}\n{% endif %}\n\n{% for product in products %}\n {% assign product_template_suffix_to_apply = nil %}\n\n {% for keyval in product_tags_and_template_suffixes %}\n {% assign product_tag_to_check = keyval[0] %}\n {% assign product_template_to_apply = keyval[1] %}\n\n {% if product.tags contains product_tag_to_check %}\n {% assign product_template_suffix_to_apply = product_template_to_apply %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% if product_template_suffix_to_apply != product.templateSuffix %}\n {% action \"shopify\" %}\n mutation {\n productUpdate(\n input: {\n id: {{ product.id | json }}\n templateSuffix: {{ product_template_suffix_to_apply | json }}\n }\n ) {\n product {\n id\n templateSuffix\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n{% endfor %}\n",
"subscriptions": [
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation",
"shopify/products/create",
"shopify/products/update"
"shopify/products/update",
"mechanic/user/trigger",
"mechanic/shopify/bulk_operation"
],
"subscriptions_template": "mechanic/user/trigger\nmechanic/shopify/bulk_operation\nshopify/products/create\nshopify/products/update\n",
"tags": [
Expand Down

0 comments on commit 7677e03

Please sign in to comment.