Update SKU

Updating an existing SKU will set the product type to `Advanced` for the product associated with the SKU. The product type is used to determine which Product and SKU fields are shown to users in the `Designer` and the `Editor`. Setting it to `Advanced` ensures that all Product and SKU fields will be shown. The product type can be edited in the `Designer` or the `Editor`. Required scope | `ecommerce:write`

Script webflow Verified

by hugo697 ยท 11/5/2024

The script

Submitted by hugo697 Bun
Verified 563 days ago
1
//native
2
type Webflow = {
3
	token: string
4
}
5

6
export async function main(
7
	auth: Webflow,
8
	site_id: string,
9
	product_id: string,
10
	sku_id: string,
11
	body: {
12
		publishStatus?: 'staging' | 'live'
13
		sku: {
14
			id?: string
15
			cmsLocaleId?: string
16
			lastPublished?: string
17
			lastUpdated?: string
18
			createdOn?: string
19
			fieldData?: {
20
				'sku-values'?: {}
21
				name: string
22
				slug: string
23
				price: { value?: number; unit?: string }
24
				'compare-at-price'?: { value?: number; unit?: string }
25
				'ec-sku-billing-method'?: 'one-time' | 'subscription'
26
				'ec-sku-subscription-plan'?: {
27
					interval?: 'day' | 'week' | 'month' | 'year'
28
					frequency?: number
29
					trial?: number
30
					plans?: {
31
						platform?: 'stripe'
32
						id?: string
33
						status?: 'active' | 'inactive' | 'canceled'
34
					}[]
35
				}
36
				'track-inventory'?: false | true
37
				quantity?: number
38
			}
39
		}
40
	}
41
) {
42
	const url = new URL(
43
		`https://api.webflow.com/v2/sites/${site_id}/products/${product_id}/skus/${sku_id}`
44
	)
45

46
	const response = await fetch(url, {
47
		method: 'PATCH',
48
		headers: {
49
			'Content-Type': 'application/json',
50
			Authorization: 'Bearer ' + auth.token
51
		},
52
		body: JSON.stringify(body)
53
	})
54

55
	if (!response.ok) {
56
		const text = await response.text()
57
		throw new Error(`${response.status} ${text}`)
58
	}
59

60
	return await response.json()
61
}
62