commerce-kit 0.65.0 → 0.67.0
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/README.md +98 -1
- package/dist/api-types.d.ts +614 -1
- package/dist/index.d.ts +63 -8
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/uploads.d.ts +54 -0
- package/dist/uploads.js +23 -0
- package/dist/uploads.js.map +1 -0
- package/package.json +7 -1
package/README.md
CHANGED
|
@@ -38,6 +38,70 @@ const { data, meta } = await commerce.productBrowse({
|
|
|
38
38
|
const product = await commerce.productGet({ idOrSlug: 'running-shoes' });
|
|
39
39
|
```
|
|
40
40
|
|
|
41
|
+
## Custom fields
|
|
42
|
+
|
|
43
|
+
Merchant-defined fields on products and variants: material, scent notes, lens width. A field
|
|
44
|
+
is defined once per store; every product or variant then holds its own value under
|
|
45
|
+
`metafields`. Variant `attributes` keep coming back, derived from the variant's fields.
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
// Define a field, then write values with the product or variant
|
|
49
|
+
await commerce.metafieldDefinitionCreate({
|
|
50
|
+
ownerType: 'product',
|
|
51
|
+
name: 'Material',
|
|
52
|
+
type: 'choice',
|
|
53
|
+
options: [{ label: 'Wool' }, { label: 'Silk' }],
|
|
54
|
+
storefrontVisible: true,
|
|
55
|
+
filterable: true,
|
|
56
|
+
});
|
|
57
|
+
await commerce.productUpdate({ idOrSlug: 'classic-tee' }, { metafields: [{ key: 'material', value: 'wool' }] });
|
|
58
|
+
|
|
59
|
+
// Read: a product carries its fields and its variants' fields
|
|
60
|
+
const product = await commerce.productGet({ idOrSlug: 'classic-tee' });
|
|
61
|
+
product?.metafields; // [{ key: 'material', name: 'Material', type: 'choice', value: 'wool', options: [{ key: 'wool', label: 'Wool' }, …], hidden: false }]
|
|
62
|
+
|
|
63
|
+
// Lists carry values only when asked, and can be narrowed by filterable fields
|
|
64
|
+
const { data } = await commerce.productBrowse({
|
|
65
|
+
active: true,
|
|
66
|
+
include: 'metafields',
|
|
67
|
+
filter: 'product.custom.material:wool,silk;variant.custom.weight:100..300',
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Filter facets list the filterable fields with their options or number range
|
|
71
|
+
const { metafields } = await commerce.productFilters({ lang: 'de-DE' });
|
|
72
|
+
|
|
73
|
+
// Private fields need a key with `catalog:write`
|
|
74
|
+
const full = await commerce.productGet({ idOrSlug: 'classic-tee', metafields: 'all' });
|
|
75
|
+
|
|
76
|
+
// Manage fields: archive keeps every value, delete is for archived fields only
|
|
77
|
+
const { data: fields } = await commerce.metafieldDefinitionBrowse({ ownerType: 'variant' });
|
|
78
|
+
await commerce.metafieldDefinitionUpdate({ idOrKey: 'product.custom.material' }, { filterable: false });
|
|
79
|
+
await commerce.metafieldDefinitionArchive({ idOrKey: 'material' });
|
|
80
|
+
await commerce.metafieldDefinitionRestore({ idOrKey: 'material' });
|
|
81
|
+
await commerce.metafieldDefinitionDelete({ idOrKey: 'material' }, { confirm: 'material', expectedValueCount: 12 });
|
|
82
|
+
|
|
83
|
+
// Turn a text field into a choice, in place: preview, then confirm the grouping
|
|
84
|
+
const preview = await commerce.metafieldDefinitionConversionPreview({ idOrKey: 'variant.custom.scent' });
|
|
85
|
+
await commerce.metafieldDefinitionConvertToChoice(
|
|
86
|
+
{ idOrKey: 'variant.custom.scent' },
|
|
87
|
+
{ groups: preview.suggestedGroups, planHash: preview.planHash },
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
// Translations: field names and option labels, then the values of a product or variant
|
|
91
|
+
await commerce.metafieldDefinitionTranslationPut(
|
|
92
|
+
{ idOrKey: 'product.custom.material', locale: 'de-DE' },
|
|
93
|
+
{ name: 'Material', optionLabels: { wool: 'Wolle', silk: 'Seide' } },
|
|
94
|
+
);
|
|
95
|
+
await commerce.productMetafieldTranslationsPut(
|
|
96
|
+
{ idOrSlug: 'classic-tee', locale: 'de-DE' },
|
|
97
|
+
{ values: [{ key: 'care', value: 'Nur Handwäsche' }] },
|
|
98
|
+
);
|
|
99
|
+
await commerce.metafieldsTranslateMissing({ locales: ['de-DE'] }); // machine, never over a person's text
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Addressing a field: its id, or `<ownerType>.<namespace>.<key>` such as `product.custom.material`.
|
|
103
|
+
The deprecated variant `attributes` input is still accepted on writes and stored as custom fields.
|
|
104
|
+
|
|
41
105
|
## Cart
|
|
42
106
|
|
|
43
107
|
```typescript
|
|
@@ -51,13 +115,46 @@ const cart = await commerce.cartUpsert({
|
|
|
51
115
|
// Get cart
|
|
52
116
|
const cart = await commerce.cartGet({ cartId: 'cart_123' });
|
|
53
117
|
|
|
54
|
-
// Remove item from cart
|
|
118
|
+
// Remove item from cart (pass lineItemId to remove one personalised line of the variant)
|
|
55
119
|
await commerce.cartRemoveItem({
|
|
56
120
|
cartId: 'cart_123',
|
|
57
121
|
variantId: 'variant_456',
|
|
58
122
|
});
|
|
59
123
|
```
|
|
60
124
|
|
|
125
|
+
### Personalised lines and shopper uploads
|
|
126
|
+
|
|
127
|
+
A line can carry `properties`: text (an engraving) or an uploaded file (a photo for a portrait).
|
|
128
|
+
The same variant with different properties becomes its own line.
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
await commerce.cartUpsert({
|
|
132
|
+
variantId: 'variant_456',
|
|
133
|
+
quantity: 1,
|
|
134
|
+
properties: [{ key: 'engraving', label: 'Engraving', type: 'text', value: 'For Ana' }],
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Files are uploaded from the browser with `commerce-kit/uploads` (needs the store's Customer Uploads
|
|
139
|
+
module). The platform endpoint is reached through the storefront's own `/checkout/api/**` proxy, so no
|
|
140
|
+
route or API key is needed in the storefront. Images (JPEG, PNG, WebP, GIF, HEIC, AVIF, TIFF) and PDF,
|
|
141
|
+
up to 20 MB, stored privately.
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
'use client';
|
|
145
|
+
import { ACCEPTED_LINE_ITEM_UPLOAD_TYPES, lineItemFileProperty, uploadLineItemFile } from 'commerce-kit/uploads';
|
|
146
|
+
|
|
147
|
+
// <input type="file" accept={ACCEPTED_LINE_ITEM_UPLOAD_TYPES} onChange={...} />
|
|
148
|
+
const { uploadId } = await uploadLineItemFile(file, { variantId });
|
|
149
|
+
// then, on the server (server action):
|
|
150
|
+
await commerce.cartUpsert({
|
|
151
|
+
cartId,
|
|
152
|
+
variantId,
|
|
153
|
+
quantity: 1,
|
|
154
|
+
properties: [lineItemFileProperty({ key: 'photo', label: 'Photo', uploadId })],
|
|
155
|
+
});
|
|
156
|
+
```
|
|
157
|
+
|
|
61
158
|
## Orders
|
|
62
159
|
|
|
63
160
|
```typescript
|