-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-full-orders.js
149 lines (135 loc) · 3.52 KB
/
get-full-orders.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const {
default: WooCommerceRestApi,
} = require("@woocommerce/woocommerce-rest-api");
const fs = require("fs");
const api = new WooCommerceRestApi({
url: "https://api.feuerschutz.ch",
consumerKey: process.argv[2],
consumerSecret: process.argv[3],
version: "wc/v3",
});
const SEPERATOR = '"' + "," + '"';
const FILENAME = "orders.csv";
const HEADERS = [
"Bestellnummer",
"Bestelldatum",
"Total",
"Total Steuern",
"Versandkosten",
"Kunden ID",
"Notiz",
"Rechnung Vorname",
"Rechnung Nachname",
"Rechnung Firma",
"Rechnung Adresse 1",
"Rechnung Adresse 2",
"Rechnung Postfach",
"Rechnung Stadt",
"Rechnung Kanton",
"Rechnung Postleitzahl",
"Rechnung Land",
"Rechnung E-Mail",
"Rechnung Telefon",
"Versand Vorname",
"Versand Nachname",
"Versand Firma",
"Versand Adresse 1",
"Versand Adresse 2",
"Versand Postfach",
"Versand Stadt",
"Versand Kanton",
"Versand Postleitzahl",
"Versand Land",
"Produkt Name",
"Produkt SKU",
"Produkt Preis",
"Produkt Menge",
"Produkt Subtotal",
];
const generateOrderCsv = (orders) =>
[].concat
.apply(
[],
orders.map((o) =>
o.line_items.map(
(p) =>
'"' +
[
o.id,
o.date_created,
o.total,
o.total_tax,
o.shipping_total,
o.customer_id,
o.customer_note,
o.billing.first_name,
o.billing.last_name,
o.billing.company,
o.billing.address_1,
o.billing.address_2,
o.billing.post_office_box,
o.billing.city,
o.billing.state,
o.billing.postcode,
o.billing.country,
o.billing.email,
o.billing.phone,
o.shipping.first_name,
o.shipping.last_name,
o.shipping.company,
o.shipping.address_1,
o.shipping.address_2,
o.shipping.post_office_box,
o.shipping.city,
o.shipping.state,
o.shipping.postcode,
o.shipping.country,
p.name,
p.sku,
p.price,
p.quantity,
p.subtotal,
].join(SEPERATOR) +
'"'
)
)
)
.join("\n");
const generateHeader = () => '"' + HEADERS.join(SEPERATOR) + '"';
const fetchOrders = async () => {
const firstRun = !fs.existsSync(`./${FILENAME}`);
let lastRun = new Date(0);
if (firstRun) {
const header = generateHeader();
fs.writeFileSync(`./${FILENAME}`, header, { encoding: "utf8" });
} else {
const stats = fs.statSync(`./${FILENAME}`);
lastRun = stats.mtime;
}
let totalOrders = 1; //just so we fetch at least once
let orders = 0;
let page = 1;
let maxProducts = 0;
while (orders < totalOrders) {
const params = { per_page: 100, page };
if (!firstRun) {
params.after = lastRun.toISOString();
}
const response = await api.get("orders", params);
totalOrders = response.headers["x-wp-total"];
fs.appendFileSync(`./${FILENAME}`, "\n" + generateOrderCsv(response.data), {
encoding: "utf8",
});
orders += response.data.length;
page++;
maxProducts = response.data.reduce(
(max, order) =>
order.line_items.length > max ? order.line_items.length : max,
maxProducts
);
console.log(
`Fetched ${orders} orders of ${totalOrders} after ${lastRun.toDateString()}`
);
}
};
fetchOrders();