Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

second batch of REST to GraphQL conversions for products and variants #404

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/auto-generate-skus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ To illustrate, a shirt available at `myshop.com/products/stylish-shirt-503`, wit
* XL, Heather gray: `503-XL-H`
* Small, Red: `503-S-R`

To update your product handle, so as to control the first portion of generated SKUs, open the product in the Shopify admin, scroll to the bottom of the page, click "Edit website SEO", and update the "URL and handle" field to taste. :)
To update your product handle, so as to control the first portion of generated SKUs, open the product in the Shopify admin, scroll to the bottom of the page, click the edit icon next to "Search engine listing", and update the "URL handle" field to taste. :)

## Installing this task

Expand Down
152 changes: 97 additions & 55 deletions docs/auto-generate-skus/script.liquid
Original file line number Diff line number Diff line change
@@ -1,71 +1,109 @@
{% assign skip_variants_that_already_have_skus = options.skip_variants_that_already_have_skus__boolean %}
{% assign product_options_to_keep_unabbreviated = options.product_options_to_keep_unabbreviated__array %}

{% assign allowed_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" %}

{% assign sku_base = product.handle | split: "-" | last | upcase %}

{% if event.preview %}
{% capture product_json %}
{
"handle": "stylish-shirt-503",
"variants": [
{
"sku": "503-L-B",
"option1": "L",
"option2": "Black",
"admin_graphql_api_id": "gid://shopify/ProductVariant/1234567890"
},
{
"sku": "503-XL-G",
"option1": "XL",
"option2": "Heather Grey",
"admin_graphql_api_id": "gid://shopify/ProductVariant/2345678901"
{% if options.skip_variants_that_already_have_skus__boolean %}
},
{
"option1": "XXL",
"option2": "Red",
"admin_graphql_api_id": "gid://shopify/ProductVariant/3456789012"
{% endif %}
}
],
"options": [
{
"name": "Size"
},
{
"name": "Color"
{% assign sku_base = "503" %}
{% endif %}

{% assign cursor = nil %}
{% assign variants = array %}

{% for n in (1..8) %}
{% capture query %}
query {
product(id: {{ product.admin_graphql_api_id | json }}) {
id
handle
variants(
first: 250
after: {{ cursor | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
inventoryItem {
id
}
sku
selectedOptions {
name
value
}
}
}
]
}
}
{% endcapture %}

{% assign product = product_json | parse_json %}
{% endif %}
{% assign result = query | shopify %}

{% assign allowed_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" %}
{% if event.preview %}
{% capture result_json %}
{
"data": {
"product": {
"variants": {
"nodes": [
{
"inventoryItem": {
"id": "gid://shopify/InventoryItem/1234567890"
},
{% unless skip_variants_that_already_have_skus %}"sku": "503-XL-G",{% endunless %}
"selectedOptions": [
{
"name": "Size",
"value": "XL"
},
{
"name": "Color",
"value": "Heather Grey"
}
]
}
]
}
}
}
}
{% endcapture %}

{% assign sku_base = product.handle | split: "-" | last | upcase %}
{% assign result = result_json | parse_json %}
{% endif %}

{% for variant in product.variants %}
{% if options.skip_variants_that_already_have_skus__boolean and variant.sku != blank %}
{% log message: "variant already has a sku; skipping", original_sku: variant.sku %}
{% assign variants = variants | concat: result.data.product.variants.nodes %}

{% if result.data.product.variants.pageInfo.hasNextPage %}
{% assign cursor = result.data.product.variants.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}

{% for variant in variants %}
{% if skip_variants_that_already_have_skus and variant.sku != blank %}
{% log
message: "variant already has a sku; skipping",
original_sku: variant.sku
%}
{% continue %}
{% endif %}

{% assign variant_sku_generated = sku_base %}

{% assign variant_options = array %}
{% assign variant_options[0] = variant.option1 %}
{% assign variant_options[1] = variant.option2 %}
{% assign variant_options[2] = variant.option3 %}
{% for selected_option in variant.selectedOptions %}
{% if product_options_to_keep_unabbreviated contains selected_option.name %}
{% assign variant_sku_generated = variant_sku_generated | append: "-" | append: selected_option.value %}

{% assign variant_option_names = product.options | map: "name" %}

{% for variant_option in variant_options %}
{% if variant_option == blank %}
{% continue %}
{% endif %}

{% if options.product_options_to_keep_unabbreviated__array contains variant_option_names[forloop.index0] %}
{% assign variant_sku_generated = variant_sku_generated | append: "-" | append: variant_option %}
{% else %}
{% assign variant_option_abbreviation = "" %}
{% assign variant_option_characters = variant_option | split: "" %}
{% assign variant_option_characters = selected_option.value | split: "" %}

{% for variant_option_character in variant_option_characters %}
{% if allowed_characters contains variant_option_character %}
{% assign variant_option_abbreviation = variant_option_abbreviation | append: variant_option_character %}
Expand All @@ -81,16 +119,20 @@
{% endfor %}

{% if variant_sku_generated != variant.sku %}
{% log message: "updating sku", original_sku: variant.sku, generated_sku: variant_sku_generated %}
{% log
message: "updating sku",
original_sku: variant.sku,
generated_sku: variant_sku_generated
%}

{% comment %}
-- update skus on inventory items, as this is no longer supported on variants as of 2024-07 API
-- as of the 2024-07 API, SKUs are updated on inventory items instead of variants
{% endcomment %}

{% action "shopify" %}
mutation {
inventoryItemUpdate(
id: {{ variant.inventory_item.admin_graphql_api_id | json }}
id: {{ variant.inventoryItem.id | json }}
input: {
sku: {{ variant_sku_generated | json }}
}
Expand Down
139 changes: 122 additions & 17 deletions docs/auto-tag-orders-by-product-collections/script.liquid
Original file line number Diff line number Diff line change
@@ -1,40 +1,144 @@
{% if event.topic contains "shopify/orders/" %}
{% assign collections_and_tags = options.collections_and_tags__keyval_required %}

{% if event.topic == "shopify/orders/create" %}
{% assign existing_tags = order.tags | split: ", " %}
{% assign tags_to_add = array %}

{% comment %}
-- get product IDs from order line items to build the collections search query
{% endcomment %}

{% assign order_product_ids = order.line_items | map: "product_id" | compact | uniq %}

{% if order_product_ids == blank %}
{% unless event.preview %}
{% log "This order does not contain any products from the shop catalog." %}
{% break %}
{% endunless %}
{% endif %}

{% assign search_query_parts = array %}

{% for order_product_id in order_product_ids %}
{% assign search_query_parts[search_query_parts.size] = order_product_id | prepend: "product_id:" %}
{% endfor %}

{% assign search_query = search_query_parts | join: " OR " %}

{% unless event.preview %}
{% log search_query: search_query %}
{% endunless %}

{% comment %}
-- get collections that contain any of this order's products
{% endcomment %}

{% assign cursor = nil %}
{% assign order_collections = array %}

{% for line_item in order.line_items %}
{% if line_item.product != blank %}
{% assign order_collections = order_collections | concat: line_item.product.collections %}
{% for n in (1..10) %}
{% capture query %}
query {
collections(
first: 250
after: {{ cursor | json }}
query: {{ search_query | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
legacyResourceId
title
handle
}
}
}
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
{% capture result_json %}
{
"data": {
"collections": {
"nodes": [
{
"id": "gid://shopify/Collection/1234567890",
"legacyResourceId": "1234567890",
"title": "Best Widgets",
"handle": "best-widgets"
}
]
}
}
}
{% endcapture %}

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

{% assign order_collections = order_collections | concat: result.data.collections.nodes %}

{% if result.data.collections.pageInfo.hasNextPage %}
{% assign cursor = result.data.collections.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}

{% assign order_collection_ids = order_collections | map: "id" | join: "," | split: "," %}
{% if order_collections == blank %}
{% unless event.preview %}
{% log "This order does not contain any products that belong to any collections." %}
{% break %}
{% endunless %}
{% endif %}

{% comment %}
-- save the collection IDs, titles, and handles for comparison against the configured collections
{% endcomment %}

{% assign order_collection_ids = order_collections | map: "legacyResourceId" %}
{% assign order_collection_titles = order_collections | map: "title" %}
{% assign order_collection_handles = order_collections | map: "handle" %}

{% for pair in options.collections_and_tags__keyval_required %}
{% log
order_collection_ids: order_collection_ids,
order_collection_titles: order_collection_titles,
order_collection_handles: order_collection_handles
%}

{% comment %}
-- add order tags for any matched collections
{% endcomment %}

{% assign tags_to_add = array %}

{% for pair in collections_and_tags %}
{% assign required_collection = pair[0] %}
{% assign tags = pair[1] | split: "," %}

{% if order_collection_ids contains required_collection
or order_collection_titles contains required_collection
or order_collection_handles contains required_collection
or order_collection_titles contains required_collection
or order_collection_handles contains required_collection
%}
{% for tag in tags %}
{% assign stripped_tag = tag | strip %}

{% unless existing_tags contains stripped_tag %}
{% assign tags_to_add[tags_to_add.size] = stripped_tag %}
{% assign tags_to_add = tags_to_add | push: stripped_tag %}
{% endunless %}
{% endfor %}
{% endif %}
{% endfor %}

{% if event.preview %}
{% assign tags_to_add[0] = options.collections_and_tags__keyval_required.first.last %}
{% assign tags_to_add[0] = collections_and_tags.first.last %}
{% endif %}

{% if tags_to_add != empty %}
{% if tags_to_add != blank %}
{% action "shopify" %}
mutation {
tagsAdd(
Expand Down Expand Up @@ -134,16 +238,17 @@
{% assign order_collection_titles = order_collections | map: "title" %}
{% assign order_collection_handles = order_collections | map: "handle" %}

{% for pair in options.collections_and_tags__keyval_required %}
{% for pair in collections_and_tags %}
{% assign required_collection = pair[0] %}
{% assign tags = pair[1] | split: "," %}

{% if order_collection_ids contains required_collection
or order_collection_titles contains required_collection
or order_collection_handles contains required_collection
or order_collection_titles contains required_collection
or order_collection_handles contains required_collection
%}
{% for tag in tags %}
{% assign stripped_tag = tag | strip %}

{% unless order.tags contains stripped_tag %}
{% assign tags_to_add[tags_to_add.size] = stripped_tag %}
{% endunless %}
Expand All @@ -152,10 +257,10 @@
{% endfor %}

{% if event.preview %}
{% assign tags_to_add[0] = options.collections_and_tags__keyval_required.first.last %}
{% assign tags_to_add[0] = collections_and_tags.first.last %}
{% endif %}

{% if tags_to_add != empty %}
{% if tags_to_add != blank %}
{% action "shopify" %}
mutation {
tagsAdd(
Expand Down
4 changes: 0 additions & 4 deletions docs/auto-tag-products-by-their-options/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ mechanic/shopify/bulk_operation

Use this task to add tags to your products, based on their options, for easy filtering. A shirt might be tagged with "Color-Blue" and "Size-XL", for example. Optionally, choose to ignore options that only appear on out-of-stock variants, or choose to apply all tags in lowercase.

Use this task to add tags to your products, based on their options, for easy filtering. A shirt might be tagged with "Color-Blue" and "Size-XL", for example.

Optionally, choose to ignore options that only appear on out-of-stock variants, or choose to apply all tags in lowercase.

Change the separator to change the way tags are built. Using a dash results in "Color-Blue", an underscore results in "Color_Blue", and a colon with a space yields "Color: Blue".

This task will remove option tags that are no longer applicable, by scanning for tag prefixes using the list of product options to consider.
Expand Down
Loading