0
Create Order
One script reply has been approved by the moderators Verified

Creates a new order. See the docs

Created by hugo697 409 days ago Viewed 19596 times
0
Submitted by hugo697 Bun
Verified 409 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
  order: {
14
    payment_method: string;
15
    payment_method_title: string;
16
    set_paid: boolean;
17
    billing: {
18
      first_name: string;
19
      last_name: string;
20
      address_1: string;
21
      address_2: string;
22
      city: string;
23
      state: string;
24
      postcode: string;
25
      country: string;
26
      email: string;
27
      phone: string;
28
    };
29
    shipping: {
30
      first_name: string;
31
      last_name: string;
32
      address_1: string;
33
      address_2: string;
34
      city: string;
35
      state: string;
36
      postcode: string;
37
      country: string;
38
    };
39
    line_items: {
40
      product_id: number;
41
      variation_id?: number;
42
      quantity: number;
43
    }[];
44
    shipping_lines: {
45
      method_id: string;
46
      method_title: string;
47
      total: string;
48
    }[];
49
  }
50
) {
51
  const WooCommerce = new WooCommerceRestApi(resource);
52

53
  try {
54
    const response = await WooCommerce.post('orders', order);
55
    return response.data;
56
  } catch (error) {
57
    return {
58
      error: true,
59
      message: error.response.data || 'Internal Server Error',
60
    };
61
  }
62
}
63