Skip to content

Commit

Permalink
split product title by spaces for array comparison of words
Browse files Browse the repository at this point in the history
  • Loading branch information
tekhaus committed Oct 3, 2023
1 parent a590493 commit e10bef8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/set-product-types-by-title-keywords/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Use this task to quickly reset product types in bulk based on product titles. Wh
```json
{
"product_types_and_keywords__keyval_multiline_required": {
"Shirts": "shirt\nt-shirt\ntee"
"Shirts": "shirt\nshirts\nt-shirt\nt-shirts\ntee\ntees"
}
}
```
Expand Down
21 changes: 13 additions & 8 deletions docs/set-product-types-by-title-keywords/script.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% if event.preview %}
{% capture product_types_and_keywords_json %}
{
"Shoes": "shoe\nshoes"
"Shirts": "shirt\nshirts\nt-shirt\nt-shirts\ntee\ntees"
}
{% endcapture %}

Expand Down Expand Up @@ -50,8 +50,8 @@
"nodes": [
{
"id": "gid://shopify/Product/1234567890",
"title": "Alpha Shoe",
"productType": "Shirts"
"title": "Super soft tees",
"productType": "Shoes"
}
]
}
Expand All @@ -69,9 +69,14 @@
{% for product in result.data.products.nodes %}
{% comment %}
-- use downcase on product title and configured keywords since the "contains" operator is case-sensitive
-- split on spaces in title to create an array of words to be compared to keywords
{% endcomment %}

{% assign product_title_downcase = product.title | downcase %}
{% assign product_title_words_downcase
= product.title
| downcase
| split: " "
%}
{% assign product_type_to_set = nil %}

{% for keyval in product_types_and_keywords %}
Expand All @@ -89,15 +94,15 @@

{% assign keyword_downcase = keyword | downcase %}

{% if product_title_downcase contains keyword_downcase %}
{% if product_title_words_downcase contains keyword_downcase %}
{% assign product_type_to_set = product_type %}
{% break %}
{% endif %}
{% endfor %}

{% if product_type_to_set != blank %}
{% break %}
{% endif %}
{% endif %}
{% endfor %}

{% comment %}
Expand All @@ -123,8 +128,8 @@
}
}
}
{% endaction %}
{% endif %}
{% endaction %}
{% endif %}
{% endfor %}

{% if result.data.products.pageInfo.hasNextPage %}
Expand Down
4 changes: 2 additions & 2 deletions tasks/set-product-types-by-title-keywords.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"online_store_javascript": null,
"options": {
"product_types_and_keywords__keyval_multiline_required": {
"Shirts": "shirt\nt-shirt\ntee"
"Shirts": "shirt\nshirts\nt-shirt\nt-shirts\ntee\ntees"
}
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [],
"script": "{% assign product_types_and_keywords = options.product_types_and_keywords__keyval_multiline_required %}\n\n{% comment %}\n -- set preview values for the configuration field that will work with the preview query data\n{% endcomment %}\n\n{% if event.preview %}\n {% capture product_types_and_keywords_json %}\n {\n \"Shoes\": \"shoe\\nshoes\"\n }\n {% endcapture %}\n\n {% assign product_types_and_keywords = product_types_and_keywords_json | parse_json %}\n{% endif %}\n\n{% assign cursor = nil %}\n\n{% comment %}\n -- query for all products in the shop (if > 25K products, the \"100\" loop value can be adjusted upward)\n{% endcomment %}\n\n{% for n in (1..100) %}\n {% capture query %}\n query {\n products(\n first: 250\n after: {{ cursor | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n nodes {\n id\n title\n productType\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"products\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/Product/1234567890\",\n \"title\": \"Alpha Shoe\",\n \"productType\": \"Shirts\"\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% comment %}\n -- process each product in this result before querying for more products\n {% endcomment %}\n\n {% for product in result.data.products.nodes %}\n {% comment %}\n -- use downcase on product title and configured keywords since the \"contains\" operator is case-sensitive\n {% endcomment %}\n\n {% assign product_title_downcase = product.title | downcase %}\n {% assign product_type_to_set = nil %}\n\n {% for keyval in product_types_and_keywords %}\n {% assign product_type = keyval.first %}\n {% assign keywords = keyval.last | split: newline %}\n\n {% for keyword in keywords %}\n {% if keyword == blank or keyword == \"\" %}\n {% comment %}\n -- protect against accidental empty keyword lines in the task config\n {% endcomment %}\n\n {% continue %}\n {% endif %}\n\n {% assign keyword_downcase = keyword | downcase %}\n\n {% if product_title_downcase contains keyword_downcase %}\n {% assign product_type_to_set = product_type %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% if product_type_to_set != blank %}\n {% break %}\n {% endif %} \n {% endfor %}\n\n {% comment %}\n -- set a new product type if a keyword match was made and the product does not already have that type\n {% endcomment %}\n\n {% if product_type_to_set != blank and product_type_to_set != product.productType %}\n {% action \"shopify\" %}\n mutation {\n productUpdate(\n input: {\n id: {{ product.id | json }}\n productType: {{ product_type_to_set | json }}\n }\n ) {\n product {\n title\n productType\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %} \n {% endif %} \n {% endfor %}\n\n {% if result.data.products.pageInfo.hasNextPage %}\n {% assign cursor = result.data.products.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n{% endfor %}",
"script": "{% assign product_types_and_keywords = options.product_types_and_keywords__keyval_multiline_required %}\n\n{% comment %}\n -- set preview values for the configuration field that will work with the preview query data\n{% endcomment %}\n\n{% if event.preview %}\n {% capture product_types_and_keywords_json %}\n {\n \"Shirts\": \"shirt\\nshirts\\nt-shirt\\nt-shirts\\ntee\\ntees\"\n }\n {% endcapture %}\n\n {% assign product_types_and_keywords = product_types_and_keywords_json | parse_json %}\n{% endif %}\n\n{% assign cursor = nil %}\n\n{% comment %}\n -- query for all products in the shop (if > 25K products, the \"100\" loop value can be adjusted upward)\n{% endcomment %}\n\n{% for n in (1..100) %}\n {% capture query %}\n query {\n products(\n first: 250\n after: {{ cursor | json }}\n ) {\n pageInfo {\n hasNextPage\n endCursor\n }\n nodes {\n id\n title\n productType\n }\n }\n }\n {% endcapture %}\n\n {% assign result = query | shopify %}\n\n {% if event.preview %}\n {% capture result_json %}\n {\n \"data\": {\n \"products\": {\n \"nodes\": [\n {\n \"id\": \"gid://shopify/Product/1234567890\",\n \"title\": \"Super soft tees\",\n \"productType\": \"Shoes\"\n }\n ]\n }\n }\n }\n {% endcapture %}\n\n {% assign result = result_json | parse_json %}\n {% endif %}\n\n {% comment %}\n -- process each product in this result before querying for more products\n {% endcomment %}\n\n {% for product in result.data.products.nodes %}\n {% comment %}\n -- use downcase on product title and configured keywords since the \"contains\" operator is case-sensitive\n -- split on spaces in title to create an array of words to be compared to keywords\n {% endcomment %}\n\n {% assign product_title_words_downcase\n = product.title\n | downcase\n | split: \" \"\n %}\n {% assign product_type_to_set = nil %}\n\n {% for keyval in product_types_and_keywords %}\n {% assign product_type = keyval.first %}\n {% assign keywords = keyval.last | split: newline %}\n\n {% for keyword in keywords %}\n {% if keyword == blank or keyword == \"\" %}\n {% comment %}\n -- protect against accidental empty keyword lines in the task config\n {% endcomment %}\n\n {% continue %}\n {% endif %}\n\n {% assign keyword_downcase = keyword | downcase %}\n\n {% if product_title_words_downcase contains keyword_downcase %}\n {% assign product_type_to_set = product_type %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% if product_type_to_set != blank %}\n {% break %}\n {% endif %}\n {% endfor %}\n\n {% comment %}\n -- set a new product type if a keyword match was made and the product does not already have that type\n {% endcomment %}\n\n {% if product_type_to_set != blank and product_type_to_set != product.productType %}\n {% action \"shopify\" %}\n mutation {\n productUpdate(\n input: {\n id: {{ product.id | json }}\n productType: {{ product_type_to_set | json }}\n }\n ) {\n product {\n title\n productType\n }\n userErrors {\n field\n message\n }\n }\n }\n {% endaction %}\n {% endif %}\n {% endfor %}\n\n {% if result.data.products.pageInfo.hasNextPage %}\n {% assign cursor = result.data.products.pageInfo.endCursor %}\n {% else %}\n {% break %}\n {% endif %}\n{% endfor %}\n",
"subscriptions": ["mechanic/user/trigger"],
"subscriptions_template": "mechanic/user/trigger",
"tags": ["Products"]
Expand Down

0 comments on commit e10bef8

Please sign in to comment.