@porulle/adapter-woocommerce 0.74.2 → 0.74.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/catalog.js +53 -10
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/catalog.ts +55 -10
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@porulle/adapter-woocommerce",
|
|
3
|
-
"version": "0.74.
|
|
3
|
+
"version": "0.74.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"zod": "^4.1.11",
|
|
19
|
-
"@porulle/core": "0.74.
|
|
19
|
+
"@porulle/core": "0.74.3"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@types/node": "^24.5.2",
|
package/src/catalog.ts
CHANGED
|
@@ -66,8 +66,51 @@ function status(value: string | null | undefined): ChannelCatalogItem["status"]
|
|
|
66
66
|
return undefined;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
/** "pa_color", "Color" and "color" are one attribute; "Blue" and the term slug "blue" one value. */
|
|
70
|
+
function attributeKey(value: string): string {
|
|
71
|
+
return value.trim().toLowerCase().replace(/^pa_/, "").replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface OptionAxis {
|
|
75
|
+
name: string;
|
|
76
|
+
values: string[];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The option axes a variable product sells along. WooCommerce lets a variation use a global
|
|
81
|
+
* attribute (`pa_color`) by its term SLUG ("blue") while the product lists the term NAME ("Blue") —
|
|
82
|
+
* or does not list that attribute at all (measured on WooCommerce's own sample catalogue). The axes
|
|
83
|
+
* are the product's declared variation attributes plus any its variations use, and each value is
|
|
84
|
+
* spelled as the product spells it where it declares one, so every variant's values are among them.
|
|
85
|
+
*/
|
|
86
|
+
function optionAxes(product: WooProduct, variations: WooVariation[]): { axes: OptionAxis[]; resolve: (name: string, option: string) => { name: string; value: string } } {
|
|
87
|
+
const axes = new Map<string, OptionAxis>();
|
|
88
|
+
for (const attribute of product.attributes.filter((candidate) => candidate.variation === true)) {
|
|
89
|
+
axes.set(attributeKey(attribute.name), { name: attribute.name, values: [...attribute.options] });
|
|
90
|
+
}
|
|
91
|
+
const resolve = (name: string, option: string): { name: string; value: string } => {
|
|
92
|
+
const key = attributeKey(name);
|
|
93
|
+
let axis = axes.get(key);
|
|
94
|
+
if (!axis) {
|
|
95
|
+
axis = { name: name.replace(/^pa_/, "").replace(/^./, (first) => first.toUpperCase()), values: [] };
|
|
96
|
+
axes.set(key, axis);
|
|
97
|
+
}
|
|
98
|
+
const declared = axis.values.find((value) => attributeKey(value) === attributeKey(option));
|
|
99
|
+
if (declared) return { name: axis.name, value: declared };
|
|
100
|
+
const shown = option.replace(/^./, (first) => first.toUpperCase());
|
|
101
|
+
axis.values.push(shown);
|
|
102
|
+
return { name: axis.name, value: shown };
|
|
103
|
+
};
|
|
104
|
+
for (const row of variations) for (const attribute of row.attributes) if (attribute.option) resolve(attribute.name, attribute.option);
|
|
105
|
+
return { axes: [...axes.values()], resolve };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function variant(row: WooVariation, currency: string | undefined, resolve: (name: string, option: string) => { name: string; value: string }): ChannelCatalogVariant {
|
|
109
|
+
const optionValues = Object.fromEntries(row.attributes.flatMap((attribute) => {
|
|
110
|
+
if (!attribute.option) return [];
|
|
111
|
+
const resolved = resolve(attribute.name, attribute.option);
|
|
112
|
+
return [[resolved.name, resolved.value] as const];
|
|
113
|
+
}));
|
|
71
114
|
const rowPrices = prices(row, currency);
|
|
72
115
|
return {
|
|
73
116
|
externalId: row.id,
|
|
@@ -100,15 +143,17 @@ export function catalogItem(product: WooProduct, variations: WooVariation[], cur
|
|
|
100
143
|
images.push({ externalId: row.image.id, url: row.image.src, ...(row.image.alt ? { alt: row.image.alt } : {}), role: "gallery", sortOrder: images.length, variantExternalIds: [row.id] });
|
|
101
144
|
}
|
|
102
145
|
}
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
displayName: attribute.name,
|
|
106
|
-
sortOrder: attribute.position ?? index,
|
|
107
|
-
values: attribute.options.map((value, valueIndex) => ({ value, displayValue: value, sortOrder: valueIndex })),
|
|
108
|
-
}));
|
|
146
|
+
const sellable = product.type === "simple" ? [] : variations.filter((row) => row.status !== "private");
|
|
147
|
+
const { axes, resolve } = optionAxes(product, sellable);
|
|
109
148
|
const variants = product.type === "simple"
|
|
110
|
-
? [variant({ id: product.id, sku: product.sku, price: product.price, regular_price: product.regular_price, sale_price: product.sale_price, attributes: [] }, currency)]
|
|
111
|
-
:
|
|
149
|
+
? [variant({ id: product.id, sku: product.sku, price: product.price, regular_price: product.regular_price, sale_price: product.sale_price, attributes: [] }, currency, resolve)]
|
|
150
|
+
: sellable.map((row) => variant(row, currency, resolve));
|
|
151
|
+
const options = product.type === "simple" ? [] : axes.map((axis, index) => ({
|
|
152
|
+
name: axis.name,
|
|
153
|
+
displayName: axis.name,
|
|
154
|
+
sortOrder: index,
|
|
155
|
+
values: axis.values.map((value, valueIndex) => ({ value, displayValue: value, sortOrder: valueIndex })),
|
|
156
|
+
}));
|
|
112
157
|
const itemStatus = status(product.status);
|
|
113
158
|
return {
|
|
114
159
|
externalId: product.id,
|