Skip to content

Commit

Permalink
seventh batch of REST to GraphQL conversions for products and variants (
Browse files Browse the repository at this point in the history
  • Loading branch information
tekhaus authored Dec 7, 2024
1 parent 2d8817f commit 595f772
Show file tree
Hide file tree
Showing 11 changed files with 625 additions and 278 deletions.
223 changes: 145 additions & 78 deletions docs/auto-tag-products-in-a-manual-collection/script.liquid
Original file line number Diff line number Diff line change
@@ -1,116 +1,183 @@
{% assign collection_tag = options.collection_tag__required %}
{% assign collection_id = options.collection_id__number_required %}

{% assign collection_qualifies = false %}
{% assign collection_tag_lower = collection_tag | downcase %}

{% if event.topic contains "shopify/collections/" and collection.id == options.collection_id__number_required %}
{% assign collection_qualifies = true %}
{% if event.topic == "shopify/collections/update" %}
{% comment %}
-- on collection update, exit if this is not the configured collection
{% endcomment %}

{% elsif event.topic == "mechanic/user/trigger" %}
{% assign collection_qualifies = true %}
{% assign collection = shop.collections[options.collection_id__number_required] %}
{% unless collection.id == collection_id or event.preview %}
{% break %}
{% endunless %}
{% endif %}

{% if collection_qualifies or event.preview %}
{% assign collection_product_ids = collection.products | map: "admin_graphql_api_id" %}
{% comment %}
-- get all of the product IDs currently in the collection
{% endcomment %}

{% assign cursor = nil %}
{% assign tagged_product_ids = array %}
{% assign cursor = nil %}
{% assign collection_product_ids = array %}

{% for n in (0..100) %}
{% capture query %}
query {
{% for n in (1..100) %}
{% capture query %}
query {
collection(id: {{ collection_id | prepend: "gid://shopify/Collection/" | json }}) {
products(
first: 250
after: {{ cursor | json }}
query: {{ collection_tag | json | prepend: "tag:" | json }}
) {
pageInfo {
hasNextPage
endCursor
}
edges {
cursor
node {
id
}
nodes {
id
}
}
}
{% endcapture %}

{% assign result = query | shopify %}
}
{% endcapture %}

{% if event.preview %}
{% assign collection_product_ids = array %}
{% assign collection_product_ids[0] = "gid://shopify/Product/1234567890" %}
{% assign collection_product_ids[1] = "gid://shopify/Product/2345678901" %}
{% assign result = query | shopify %}

{% capture result_json %}
{
"data": {
{% if event.preview %}
{% capture result_json %}
{
"data": {
"collection": {
"products": {
"edges": [
"nodes": [
{
"node": {
"id": "gid://shopify/Product/1234567890"
}
},
{
"node": {
"id": "gid://shopify/Product/9876543210"
}
"id": "gid://shopify/Product/1234567890"
}
]
}
}
}
{% endcapture %}
}
{% endcapture %}

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

{% assign collection_product_ids
= result.data.collection.products.nodes
| map: "id"
| concat: collection_product_ids
%}

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

{% comment %}
-- get IDs for all products with collection tag
{% endcomment %}

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

{% for n in (0..100) %}
{% capture query %}
query {
products(
first: 250
after: {{ cursor | json }}
query: {{ collection_tag | json | prepend: "tag:" | json }}
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
tags
}
}
}
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
{% capture result_json %}
{
"data": {
"products": {
"nodes": [
{
"id": "gid://shopify/Product/2345678901",
"tags": {{ collection_tag | json }}
}
]
}
}
}
{% endcapture %}

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

{% assign products_result = result.data.products.edges | map: "node" | map: "id" %}
{% assign tagged_product_ids = tagged_product_ids | concat: products_result %}
{% comment %}
-- double check that the collection tag exists on each product to avoid occasional query filter quirkiness
{% endcomment %}

{% if result.data.products.pageInfo.hasNextPage %}
{% assign cursor = result.data.products.edges.last.cursor %}
{% else %}
{% break %}
{% for product in result.data.products.nodes %}
{% assign product_tags_lower = product.tags | json | downcase | parse_json %}

{% if product_tags_lower contains collection_tag_lower %}
{% assign tagged_product_ids = tagged_product_ids | push: product.id %}
{% endif %}
{% endfor %}

{% for collection_product_id in collection_product_ids %}
{% unless tagged_product_ids contains collection_product_id %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ collection_product_id | json }}
tags: {{ collection_tag | json }}
) {
userErrors {
field
message
}
{% if result.data.products.pageInfo.hasNextPage %}
{% assign cursor = result.data.products.pageInfo.endCursor %}
{% else %}
{% break %}
{% endif %}
{% endfor %}

{% comment %}
-- add and remove the collection tag from products
{% endcomment %}

{% for collection_product_id in collection_product_ids %}
{% unless tagged_product_ids contains collection_product_id %}
{% action "shopify" %}
mutation {
tagsAdd(
id: {{ collection_product_id | json }}
tags: {{ collection_tag | json }}
) {
userErrors {
field
message
}
}
{% endaction %}
{% endunless %}
{% endfor %}
}
{% endaction %}
{% endunless %}
{% endfor %}

{% for tagged_product_id in tagged_product_ids %}
{% unless collection_product_ids contains tagged_product_id %}
{% action "shopify" %}
mutation {
tagsRemove(
id: {{ tagged_product_id | json }}
tags: {{ collection_tag | json }}
) {
userErrors {
field
message
}
{% for tagged_product_id in tagged_product_ids %}
{% unless collection_product_ids contains tagged_product_id %}
{% action "shopify" %}
mutation {
tagsRemove(
id: {{ tagged_product_id | json }}
tags: {{ collection_tag | json }}
) {
userErrors {
field
message
}
}
{% endaction %}
{% endunless %}
{% endfor %}
{% endif %}
}
{% endaction %}
{% endunless %}
{% endfor %}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ Either triggered manually, or configured to run daily, this task will look for t

```liquid
mechanic/user/trigger
{% if options.run_daily__boolean %}
mechanic/scheduler/daily
{% endif %}
Expand All @@ -37,7 +36,11 @@ mechanic/user/trigger

Either triggered manually, or configured to run daily, this task will look for the oldest products in the collection of your choice, and delete as many of them as you wish.

This task requires a collection ID - [learn how to find yours](https://help.usemechanic.com/en/articles/2946120-how-do-i-find-an-id-for-a-product-collection-order-or-something-else).
**IMPORTANT**: When first configuring this task, run it manually once in "Test mode" to see a list of which products it would delete. Once verfied, be sure to uncheck this option to have the task make the deletions going forward.

*Notes:*
- The products will be deleted asycnronously by Shopify, which means they may appear in the products list of admin for a bit after a task run.
- This task requires a collection ID - [learn how to locate common resource IDs](https://learn.mechanic.dev/techniques/finding-a-resource-id).

## Installing this task

Expand Down
Loading

0 comments on commit 595f772

Please sign in to comment.