0
Update Product
One script reply has been approved by the moderators Verified

Updates a product. See the docs

Created by hugo697 400 days ago Viewed 18796 times
0
Submitted by hugo697 Bun
Verified 400 days ago
1
import WooCommerceRestApi from '@woocommerce/woocommerce-rest-api';
2

3
type WooCommerce = {
4
  url: string;
5
  consumerKey: string;
6
  consumerSecret: string;
7
  version?: string;
8
  queryStringAuth?: boolean;
9
};
10

11
export async function main(resource: WooCommerce, productId: number, updatePayload: any) {
12
  const WooCommerce = new WooCommerceRestApi(resource);
13

14
  try {
15
    const response = await WooCommerce.put(`products/${productId}`, updatePayload);
16
    return response.data;
17
  } catch (error) {
18
    return {
19
      error: true,
20
      message: error.response.data || 'Internal Server Error',
21
    }
22
  }
23
}
24