Shorten printful/webflow naming convention

This commit is contained in:
Dominic Ferrando
2026-07-04 15:28:59 -04:00
parent afdc8de77c
commit a7b8dd0bea
7 changed files with 231 additions and 231 deletions
+128 -128
View File
@@ -11,19 +11,19 @@ type ProductEntry = {
product: Printful.Products.Product;
};
type MetaPrintfulProduct = {
type PMetaProduct = {
name: string;
webflowProductId: string;
wProductId: string;
entries: ProductEntry[];
};
export type ProductSyncerOptions = {
filter?: {
printfulProductIds: number[] | null;
pProductIds: number[] | null;
},
onStart?: (startDate: Date) => Promise<void>
onCompletion?: (completionDate: Date, duration: number) => Promise<void>
onStep?: (printfulProductIds: number[]) => Promise<void>
onStep?: (pProductIds: number[]) => Promise<void>
};
export class ProductSyncer {
@@ -41,38 +41,38 @@ export class ProductSyncer {
const startDate = new Date();
await opt.onStart?.(startDate);
if (opt.filter?.printfulProductIds)
this.log.info({ printfulProductIds: opt.filter.printfulProductIds }, "sync started (filtered)");
if (opt.filter?.pProductIds)
this.log.info({ pProductIds: opt.filter.pProductIds }, "sync started (filtered)");
else
this.log.info("sync started (all)");
this.log.debug("retrieving webflow products");
const allWebflowProducts = await this.Webflow.Products.list({ forceAll: true });
const allWProducts = await this.Webflow.Products.list({ forceAll: true });
this.log.debug("retrieving printful products");
const allPrintfulProducts = await this.Printful.Products.list({ forceAll: true });
const allPProducts = await this.Printful.Products.list({ forceAll: true });
this.log.info({ printfulProductCount: allPrintfulProducts.length, filter: opt.filter ?? "N/A" }, "generating meta printful products");
const metaPrintfulProducts = await this.generateMetaPrintfulProducts(allWebflowProducts, allPrintfulProducts, opt);
this.log.info({ pProductCount: allPProducts.length, filter: opt.filter ?? "N/A" }, "generating printful meta products");
const pMetaProducts = await this.generatePMetaProducts(allWProducts, allPProducts, opt);
for (const metaPrintfulProduct of metaPrintfulProducts) {
const printfulProductIds = metaPrintfulProduct.entries.map((e) => e.product.sync_product.id);
this.log.info({ name: metaPrintfulProduct.name, externalId: metaPrintfulProduct.webflowProductId, printfulProductIds }, "syncing meta printful product");
await opt.onStep?.(printfulProductIds);
for (const pMetaProduct of pMetaProducts) {
const pProductIds = pMetaProduct.entries.map((e) => e.product.sync_product.id);
this.log.info({ name: pMetaProduct.name, externalId: pMetaProduct.wProductId, pProductIds }, "syncing printful meta product");
await opt.onStep?.(pProductIds);
const newWebflowSkus: DeepPartial<Webflow.Products.Skus.Sku>[] = this.generateWebflowSkus(metaPrintfulProduct);
const newWSkus: DeepPartial<Webflow.Products.Skus.Sku>[] = this.generateWSkus(pMetaProduct);
const foundColors = new Set<string>(
newWebflowSkus.map((sku) => sku.fieldData?.["sku-values"]?.["color"]).filter((v) => v !== undefined)
newWSkus.map((sku) => sku.fieldData?.["sku-values"]?.["color"]).filter((v) => v !== undefined)
);
const foundSizes = new Set<string>(
newWebflowSkus.map((sku) => sku.fieldData?.["sku-values"]?.["size"]).filter((v) => v !== undefined)
newWSkus.map((sku) => sku.fieldData?.["sku-values"]?.["size"]).filter((v) => v !== undefined)
);
if (!newWebflowSkus[0]) throw new Error("no webflow SKUs generated — all variants were filtered out");
if (!newWSkus[0]) throw new Error("no webflow SKUs generated — all variants were filtered out");
const webflowProductFieldData = {
name: metaPrintfulProduct.name,
slug: formatSlug(metaPrintfulProduct.name),
const wProductFieldData = {
name: pMetaProduct.name,
slug: formatSlug(pMetaProduct.name),
shippable: true,
"tax-category": "standard-taxable",
"sku-properties": [
@@ -97,101 +97,101 @@ export class ProductSyncer {
],
};
const webflowProduct = allWebflowProducts.find((wp) => wp.product.id === metaPrintfulProduct.webflowProductId);
const wProduct = allWProducts.find((wp) => wp.product.id === pMetaProduct.wProductId);
if (webflowProduct) {
this.log.info({ id: webflowProduct.product.id }, "existing webflow product found, updating it");
if (wProduct) {
this.log.info({ id: wProduct.product.id }, "existing webflow product found, updating it");
// do not update images
for (const sku of newWebflowSkus) delete sku.fieldData?.["main-image"];
for (const sku of newWSkus) delete sku.fieldData?.["main-image"];
// handle case where webflow SKU created by associated printful variant external_id update failed
if (!newWebflowSkus[0].id) {
this.log.warn({ webflowSku: newWebflowSkus[0] }, "missing webflow SKU id");
const webflowSkuMatch = this.resolveWebflowSku(
webflowProduct.skus,
newWebflowSkus[0].fieldData?.["sku-values"]?.["color"],
newWebflowSkus[0].fieldData?.["sku-values"]?.["size"],
if (!newWSkus[0].id) {
this.log.warn({ wSku: newWSkus[0] }, "missing webflow SKU id");
const wSkuMatch = this.resolveWSku(
wProduct.skus,
newWSkus[0].fieldData?.["sku-values"]?.["color"],
newWSkus[0].fieldData?.["sku-values"]?.["size"],
);
if (webflowSkuMatch)
newWebflowSkus[0].id = webflowSkuMatch.id;
if (wSkuMatch)
newWSkus[0].id = wSkuMatch.id;
else
this.log.warn({ webflowSku: newWebflowSkus[0] }, "could not find existing webflow SKU that matches");
this.log.warn({ wSku: newWSkus[0] }, "could not find existing webflow SKU that matches");
}
await this.Webflow.Products.update(webflowProduct.product.id, {
product: { fieldData: webflowProductFieldData },
sku: newWebflowSkus[0],
await this.Webflow.Products.update(wProduct.product.id, {
product: { fieldData: wProductFieldData },
sku: newWSkus[0],
});
for (const newWebflowSku of newWebflowSkus) {
const existingWebflowSku = this.resolveWebflowSku(
webflowProduct.skus,
newWebflowSku.fieldData?.["sku-values"]?.["color"],
newWebflowSku.fieldData?.["sku-values"]?.["size"],
newWebflowSku.id,
for (const newWSku of newWSkus) {
const existingWSku = this.resolveWSku(
wProduct.skus,
newWSku.fieldData?.["sku-values"]?.["color"],
newWSku.fieldData?.["sku-values"]?.["size"],
newWSku.id,
);
if (existingWebflowSku) {
if (existingWSku) {
await this.Webflow.Products.Skus.update(
metaPrintfulProduct.webflowProductId,
existingWebflowSku.id,
newWebflowSku,
pMetaProduct.wProductId,
existingWSku.id,
newWSku,
);
} else {
await this.Webflow.Products.Skus.create(
metaPrintfulProduct.webflowProductId,
[newWebflowSku],
pMetaProduct.wProductId,
[newWSku],
);
}
}
} else {
this.log.info("existing webflow product not found, creating it");
const webflowProductId = await this.Webflow.Products.create({
product: { fieldData: webflowProductFieldData },
sku: newWebflowSkus[0],
const wProductId = await this.Webflow.Products.create({
product: { fieldData: wProductFieldData },
sku: newWSkus[0],
});
metaPrintfulProduct.webflowProductId = webflowProductId;
pMetaProduct.wProductId = wProductId;
if (newWebflowSkus.length > 1)
await this.Webflow.Products.Skus.create(webflowProductId, newWebflowSkus.slice(1));
if (newWSkus.length > 1)
await this.Webflow.Products.Skus.create(wProductId, newWSkus.slice(1));
}
// Re-fetch after SKU mutations so newly created SKUs have their real IDs.
const freshWebflowProduct = await this.Webflow.Products.get(metaPrintfulProduct.webflowProductId);
if (!freshWebflowProduct) throw new Error("webflow product missing");
const freshWProduct = await this.Webflow.Products.get(pMetaProduct.wProductId);
if (!freshWProduct) throw new Error("webflow product missing");
// Update printful external IDs
for (const { colorGroup, product: printfulProduct } of metaPrintfulProduct.entries) {
for (const { colorGroup, product: pProduct } of pMetaProduct.entries) {
const expectedExternalId = colorGroup ?
`${metaPrintfulProduct.webflowProductId}-${colorGroup}` :
metaPrintfulProduct.webflowProductId;
if (printfulProduct.sync_product.external_id === expectedExternalId) continue;
`${pMetaProduct.wProductId}-${colorGroup}` :
pMetaProduct.wProductId;
if (pProduct.sync_product.external_id === expectedExternalId) continue;
const printfulVariantPatches: DeepPartial<Printful.Products.SyncVariant>[] = [];
for (const printfulVariant of printfulProduct.sync_variants) {
const webflowSku = this.resolveWebflowSku(
freshWebflowProduct.skus,
colorGroup ?? printfulVariant.color,
printfulVariant.size,
const pVariantPatches: DeepPartial<Printful.Products.SyncVariant>[] = [];
for (const pVariant of pProduct.sync_variants) {
const wSku = this.resolveWSku(
freshWProduct.skus,
colorGroup ?? pVariant.color,
pVariant.size,
);
if (webflowSku) {
printfulVariantPatches.push({
id: printfulVariant.id,
external_id: String(webflowSku.id),
if (wSku) {
pVariantPatches.push({
id: pVariant.id,
external_id: String(wSku.id),
});
}
}
await sleep(10000);
this.log.info({ printfulProduct: printfulProduct.sync_product.id, externalId: expectedExternalId }, "updating printful product's external ID");
await this.Printful.Products.update(printfulProduct.sync_product.id, {
this.log.info({ pProduct: pProduct.sync_product.id, externalId: expectedExternalId }, "updating printful product's external ID");
await this.Printful.Products.update(pProduct.sync_product.id, {
sync_product: {
id: printfulProduct.sync_product.id,
id: pProduct.sync_product.id,
external_id: expectedExternalId,
},
sync_variants: printfulVariantPatches,
sync_variants: pVariantPatches,
});
}
}
@@ -207,105 +207,105 @@ export class ProductSyncer {
return m?.[1] ?? null;
}
private generateWebflowSkus(metaPrintfulProduct: MetaPrintfulProduct): DeepPartial<Webflow.Products.Skus.Sku>[] {
const webflowSkus: DeepPartial<Webflow.Products.Skus.Sku>[] = [];
for (const { colorGroup, product: printfulProduct } of metaPrintfulProduct.entries) {
private generateWSkus(pMetaProduct: PMetaProduct): DeepPartial<Webflow.Products.Skus.Sku>[] {
const wSkus: DeepPartial<Webflow.Products.Skus.Sku>[] = [];
for (const { colorGroup, product: pProduct } of pMetaProduct.entries) {
const isColorGrouped = colorGroup !== null;
for (const printfulVariant of printfulProduct.sync_variants) {
for (const pVariant of pProduct.sync_variants) {
if (isColorGrouped) {
// Only include sizes present across every color variant.
const sizeInAll = metaPrintfulProduct.entries.every((e) =>
e.product.sync_variants.some((sv) => sv.size === printfulVariant.size)
const sizeInAll = pMetaProduct.entries.every((e) =>
e.product.sync_variants.some((sv) => sv.size === pVariant.size)
);
if (!sizeInAll) continue;
}
const color = isColorGrouped ?
colorGroup :
printfulVariant.color;
pVariant.color;
this.log.debug(
{ color, size: printfulVariant.size, retailPrice: printfulVariant.retail_price },
{ color, size: pVariant.size, retailPrice: pVariant.retail_price },
"generating webflow SKU from printful variant"
);
webflowSkus.push({
id: printfulVariant.external_id,
wSkus.push({
id: pVariant.external_id,
fieldData: {
name: printfulVariant.name,
slug: formatSlug(printfulVariant.name),
name: pVariant.name,
slug: formatSlug(pVariant.name),
"sku-values": {
color,
size: printfulVariant.size,
size: pVariant.size,
},
price: {
value: Math.round(+printfulVariant.retail_price * 100),
unit: printfulVariant.currency,
currency: printfulVariant.currency,
value: Math.round(+pVariant.retail_price * 100),
unit: pVariant.currency,
currency: pVariant.currency,
},
"main-image": PrintfulClient.Util.getVariantMainImage(printfulVariant),
"main-image": PrintfulClient.Util.getVariantMainImage(pVariant),
},
});
}
}
return webflowSkus;
return wSkus;
}
async generateMetaPrintfulProducts(
allWebflowProducts: Webflow.Products.ProductAndSkus[],
allPrintfulProducts: Printful.Products.SyncProduct[],
async generatePMetaProducts(
allWProducts: Webflow.Products.ProductAndSkus[],
allPProducts: Printful.Products.SyncProduct[],
opt: ProductSyncerOptions,
) {
const metaNameFilter = opt.filter?.printfulProductIds
?.map((id) => allPrintfulProducts.find((p) => p.id === id))
const metaNameFilter = opt.filter?.pProductIds
?.map((id) => allPProducts.find((p) => p.id === id))
?.filter((p) => p !== undefined)
?.map((p) => this.getMetaPrintfulProductName(p));
?.map((p) => this.getPMetaProductName(p));
const metaPrintfulProducts: MetaPrintfulProduct[] = [];
for (const printfulProduct of allPrintfulProducts) {
const metaName = this.getMetaPrintfulProductName(printfulProduct);
const colorGroup = this.parseColorGroup(printfulProduct.name);
const webflowProductId = this.isPrintfulProductSynced(printfulProduct, allWebflowProducts) ?
printfulProduct.external_id.split("-")[0] ?? "" :
const pMetaProducts: PMetaProduct[] = [];
for (const pProduct of allPProducts) {
const metaName = this.getPMetaProductName(pProduct);
const colorGroup = this.parseColorGroup(pProduct.name);
const wProductId = this.isPProductSynced(pProduct, allWProducts) ?
pProduct.external_id.split("-")[0] ?? "" :
"";
if (metaNameFilter && !metaNameFilter.includes(metaName)) continue;
let metaPrintfulProduct = metaPrintfulProducts.find((mp) => mp.name === metaName);
if (!metaPrintfulProduct) {
this.log.debug({ metaName, colorGroup, webflowProductId }, "generating meta printful product");
metaPrintfulProduct = {
let pMetaProduct = pMetaProducts.find((mp) => mp.name === metaName);
if (!pMetaProduct) {
this.log.debug({ metaName, colorGroup, wProductId }, "generating printful meta product");
pMetaProduct = {
name: metaName,
webflowProductId,
wProductId,
entries: [],
};
metaPrintfulProducts.push(metaPrintfulProduct);
} else if (!metaPrintfulProduct.webflowProductId && webflowProductId) {
// Prefer a non-empty webflowProductId so a newly added color doesn't shadow an already-synced color's ID.
metaPrintfulProduct.webflowProductId = webflowProductId;
pMetaProducts.push(pMetaProduct);
} else if (!pMetaProduct.wProductId && wProductId) {
// Prefer a non-empty wProductId so a newly added color doesn't shadow an already-synced color's ID.
pMetaProduct.wProductId = wProductId;
}
const fullPrintfulProduct = await this.Printful.Products.get(printfulProduct.id);
if (!fullPrintfulProduct) throw new Error(`Printful product ${printfulProduct.id} not found`);
const fullPProduct = await this.Printful.Products.get(pProduct.id);
if (!fullPProduct) throw new Error(`Printful product ${pProduct.id} not found`);
const isDuplicate = allPrintfulProducts.some((p) => p.id !== printfulProduct.id && p.name === printfulProduct.name);
if (isDuplicate) this.log.warn({ printfulProductName: printfulProduct.name }, "found duplicate printful product");
const isDuplicate = allPProducts.some((p) => p.id !== pProduct.id && p.name === pProduct.name);
if (isDuplicate) this.log.warn({ pProductName: pProduct.name }, "found duplicate printful product");
this.log.debug({ name: metaName, colorGroup }, "adding entry to meta printful product");
metaPrintfulProduct.entries.push({ colorGroup, product: fullPrintfulProduct });
this.log.debug({ name: metaName, colorGroup }, "adding entry to printful meta product");
pMetaProduct.entries.push({ colorGroup, product: fullPProduct });
}
return metaPrintfulProducts;
return pMetaProducts;
}
getMetaPrintfulProductName(printfulProduct: Printful.Products.SyncProduct): string {
const color = this.parseColorGroup(printfulProduct.name);
if (color) return printfulProduct.name.replace(`[${color}]`, "").trimEnd();
return printfulProduct.name;
getPMetaProductName(pProduct: Printful.Products.SyncProduct): string {
const color = this.parseColorGroup(pProduct.name);
if (color) return pProduct.name.replace(`[${color}]`, "").trimEnd();
return pProduct.name;
}
private resolveWebflowSku(
private resolveWSku(
existingSkus: Webflow.Products.Skus.Sku[],
color: string | undefined,
size: string | undefined,
@@ -319,7 +319,7 @@ export class ProductSyncer {
);
}
private isPrintfulProductSynced(printfulProduct: Printful.Products.SyncProduct, allWebflowProducts: Webflow.Products.ProductAndSkus[]) {
return allWebflowProducts.some((wp) => wp.product.id === printfulProduct.external_id.split("-")[0]);
private isPProductSynced(pProduct: Printful.Products.SyncProduct, allWProducts: Webflow.Products.ProductAndSkus[]) {
return allWProducts.some((wp) => wp.product.id === pProduct.external_id.split("-")[0]);
}
}