Create Product
One script reply has been approved by the moderators Verified

Creates a new product. See the docs

Created by hugo697 718 days ago Picked 5 times
Submitted by hugo697 Bun
Verified 310 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(
12
  resource: WooCommerce,
13
  product: {
14
    name: string;
15
    type: string;
16
    regular_price: string;
17
    description: string;
18
    short_description: string;
19
    categories: { id: number }[];
20
    images: { src: string }[];
21
  }
22
) {
23
  const WooCommerce = new WooCommerceRestApi(resource);
24

25
  try {
26
    const response = await WooCommerce.post('products', product);
27
    return response.data;
28
  } catch (error) {
29
    return {
30
      error: true,
31
      message: error.response.data || 'Internal Server Error',
32
    };
33
  }
34
}
35