//native
/**
* Create Smart Collection
* Create a collection whose products are picked automatically by conditions. Each condition is one object keyed by its type, e.g. {"productTag": {"relation": "TAGGED_WITH", "values": ["sale"], "matchType": "ANY"}}, {"productVendor": {"relation": "EQUALS", "values": ["Acme"], "matchType": "ANY"}}, {"variantPrice": {"relation": "LESS_THAN", "value": {"amount": "20", "currencyCode": "USD"}}}. Types: productTag, productTitle, productType, productVendor, productStatus, productCategory, variantTitle, variantPrice, variantCompareAtPrice, variantInventory, variantWeight, metafield*. The collection is created unpublished.
*/
export async function main(
auth: RT.Shopify,
title: string,
conditions: { [key: string]: any }[],
match_type: "ALL" | "ANY" | undefined,
description_html: string | undefined,
handle: string | undefined,
sort_order:
| "BEST_SELLING"
| "ALPHA_ASC"
| "ALPHA_DESC"
| "PRICE_ASC"
| "PRICE_DESC"
| "CREATED"
| "CREATED_DESC"
| "MANUAL"
| undefined
) {
const response = await fetch(
`https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
{
method: "POST",
headers: {
"X-Shopify-Access-Token": auth.token,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({
query: `mutation ($collection: CollectionCreateInput!) { collectionCreate(collection: $collection) { collection { id legacyResourceId title handle descriptionHtml sortOrder productsCount { count } sources { id title ... on CollectionConditionsSource { inclusion { matchType conditions { __typename id } } } } } userErrors { field message } } }`,
variables: {
collection: {
title,
descriptionHtml: description_html || undefined,
handle: handle || undefined,
sortOrder: sort_order || undefined,
sources: [
{
source: {
title,
inclusion: { matchType: match_type || "ALL", conditions },
},
},
],
},
},
}),
}
)
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const { data, errors } = await response.json()
if (errors) {
throw new Error(JSON.stringify(errors))
}
if (data.collectionCreate.userErrors.length) {
throw new Error(JSON.stringify(data.collectionCreate.userErrors))
}
return data.collectionCreate
}
Submitted by hugo989 3 hours ago