-
Notifications
You must be signed in to change notification settings - Fork 0
/
pokeToWordPress.js
125 lines (110 loc) · 3.68 KB
/
pokeToWordPress.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
function pokeToWordPressOrders(data, order_id) {
const encodedAuthInformation = Utilities.base64Encode(
`${apiusername}:${apipassword}`,
);
const headers = { Authorization: `Basic ${encodedAuthInformation}` };
const options = {
method: "post",
contentType: "application/json",
headers: headers, // Convert the JavaScript object to a JSON string.
payload: JSON.stringify(data),
};
const apiUrl = `https://www.${apidomain}/wp-json/wc/v3/orders/${order_id}`;
const response = UrlFetchApp.fetch(apiUrl, options);
const responseData = JSON.parse(response.getContentText());
//console.log(responseData)
if (response.getResponseCode() === 200) {
return responseData.id;
}
return "Error";
}
function pokeToWordPressProducts(data, product_id, variation_id = null) {
const encodedAuthInformation = Utilities.base64Encode(
`${apiusername}:${apipassword}`,
);
const headers = { Authorization: `Basic ${encodedAuthInformation}` };
const options = {
method: "post",
contentType: "application/json",
headers: headers,
payload: JSON.stringify(data),
};
let apiUrl = `https://www.${apidomain}/wp-json/wc/v3/products/${product_id}`;
if (variation_id) {
apiUrl += `/variations/${variation_id}`;
}
const response = UrlFetchApp.fetch(apiUrl, options);
const responseData = JSON.parse(response.getContentText());
if (response.getResponseCode() === 200) {
return responseData.id;
}
return "Error";
}
function getProductById(productId) {
const encodedAuthInformation = Utilities.base64Encode(
`${apiusername}:${apipassword}`,
);
const headers = { Authorization: `Basic ${encodedAuthInformation}` };
const apiUrl = `https://www.${apidomain}/wp-json/wc/v3/products/${productId}`;
const options = {
method: "get",
headers: headers,
};
const response = UrlFetchApp.fetch(apiUrl, options);
return JSON.parse(response.getContentText());
}
function getProductVariations(productId) {
const encodedAuthInformation = Utilities.base64Encode(
`${apiusername}:${apipassword}`,
);
const headers = { Authorization: `Basic ${encodedAuthInformation}` };
const apiUrl = `https://www.${apidomain}/wp-json/wc/v3/products/${productId}/variations`;
const options = {
method: "get",
headers: headers,
};
const response = UrlFetchApp.fetch(apiUrl, options);
return JSON.parse(response.getContentText());
}
function pokeToWooUserMeta(data, user_id) {
const encodedAuthInformation = Utilities.base64Encode(
`${apiusername}:${apipassword}`,
);
const headers = { Authorization: `Basic ${encodedAuthInformation}` };
const options = {
method: "post",
contentType: "application/json",
headers: headers, // Convert the JavaScript object to a JSON string.
payload: JSON.stringify(data),
};
const apiUrl = `https://www.${apidomain}/wp-json/wc/v3/customers/${user_id}`;
const response = UrlFetchApp.fetch(apiUrl, options);
const responseData = JSON.parse(response.getContentText());
if (response.getResponseCode() === 200) {
return responseData;
}
return "Error";
}
function pokeNoteToOrder(orderNumber, noteText) {
const apiUrl = `https://www.${apidomain}/wp-json/wc/v3/orders/${orderNumber}/notes`;
const encodedAuthInformation = Utilities.base64Encode(
`${apiusername}:${apipassword}`,
);
const headers = {
Authorization: `Basic ${encodedAuthInformation}`,
"Content-Type": "application/json",
};
const payload = { note: noteText };
const options = {
method: "post",
headers: headers,
payload: JSON.stringify(payload),
};
const response = UrlFetchApp.fetch(apiUrl, options);
const responseData = JSON.parse(response.getContentText());
//console.log(response.getResponseCode())
if (response.getResponseCode() === 201) {
//console.log(responseData)
return responseData.id;
}
}