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

Creates a new customer. See the docs

Created by hugo697 718 days ago
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
  customer: {
14
    email: string;
15
    first_name: string;
16
    last_name: string;
17
    username: string;
18
    billing: {
19
      first_name: string;
20
      last_name: string;
21
      company: string;
22
      address_1: string;
23
      address_2: string;
24
      city: string;
25
      state: string;
26
      postcode: string;
27
      country: string;
28
      email: string;
29
      phone: string;
30
    };
31
    shipping: {
32
      first_name: string;
33
      last_name: string;
34
      company: string;
35
      address_1: string;
36
      address_2: string;
37
      city: string;
38
      state: string;
39
      postcode: string;
40
      country: string;
41
    };
42
  }
43
) {
44
  const WooCommerce = new WooCommerceRestApi(resource);
45

46
  try {
47
    const response = await WooCommerce.post('customers', customer);
48
    return response.data;
49
  } catch (error) {
50
    return {
51
      error: true,
52
      message: error.response.data || 'Internal Server Error',
53
    }
54
  }
55
}
56