-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomerGroupsApp_backup_Feb15.js
321 lines (296 loc) · 13.2 KB
/
CustomerGroupsApp_backup_Feb15.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//Written 3:24pm, test after this shows
var Cxgroup;
const customerInfo = {};
const cxGrId = {};
var storeId = 98501509;
var public_token = 'Bearer public_PUb7yJJDWc5RYj1nL7aXHJFT5j5bn2hW';
//FOR LIVE SITE const products = ['613224679','613222359','613224661','613222163','613225187','613226056','JACKS>>','613233550','613231400','613223218','613232937','613221650','613234818','613223104']; //Calmag, Cleanse, Fade, Bloom, Core, Grow // Jacks Part A, B, Bloom, Epsom, RO, Ultra Violet, Finish
//For test site
const products = ['625285756','625238809', '625270554', '625285757'];
///////////To reset product prices
function resetProducts(){
alert('Resetting Products');
for (const productID of products){
console.log('FOR ' + productID + ' ');
var reqURL_Products = 'https://app.ecwid.com/api/v3/'+storeId+'/products/'+productID;
const Product = {};
// const productGET = {
// method: 'GET',
// headers: {accept:'application/json', Authorization: public_token}
// };
// const productPUT = {
// method: 'PUT',
// headers: {accept:'application/json',
// Authorization: public_token,
// 'content/type':'application/json'
// }
// };
var get = new XMLHttpRequest();
get.open("GET", reqURL_Products, true);
get.setRequestHeader("Authorization", public_token);
get.send();
get.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
Product = xhttp.responseJSON;
console.log(JSON.stringify(Product)); // prints response in format of Search Products request in Ecwid API
}else{
console.log(xhttp.readyState + '<<State | Status>> '+xhttp.statusText);
}
};
if (Product.productID = productID){
console.log('Product ids match, finding which product');
switch(productID){ //Resets product price
case productID = '625285756': //Nutrient
Product.price = '275.00';
break;
case productID = '625238809': //Additive
Product.price = '85.00';
break;
case productID = '625270554': //Humidifier
Product.price = '1500';
break;
case productID = '625285757': //Dehumidifier
Product.price = '1900';
break;
default:
console.log(productID + ' actual product id: ' + Product.productID);
}
}else{
console.log('Product ID could not match, product id is: ' + Product.productID);
}
var put = new XMLHttpRequest();
put.open("PUT", reqURL_Products, true);
put.setRequestHeader("Authorization", public_token);
put.send(JSON.stringify(Product));
put.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
var apiResponse = xhttp.responseText;
console.log(apiResponse); // prints response in format of Search Products request in Ecwid API
}else{
console.log(xhttp.readyState + '<<State | Status>> '+xhttp.statusText);
}
};
}
}
//Get the products to be enabled and disabled
function runProducts(){
console.log('in runProducts()');
//uses product id's to deactivate and activate
// const productToActivate = [];
// const productsToDeactivate = [];
//for loop required here for each product to activate and deactivate
//To alter pricing
for(const productID of products){
console.log('For product id: '+productID);
var reqURL_Products = 'https://app.ecwid.com/api/v3/'+storeId+'/products/'+productID;
const Product = {};
// const productGET = {
// method: 'GET',
// headers: {accept:'application/json', Authorization: public_token}
// };
// const productPUT = {
// method: 'PUT',
// headers: {accept:'application/json',
// Authorization: public_token,
// 'content/type':'application/json'
// }
// };
var xhttp = new XMLHttpRequest();
xhttp.open("GET", reqURL_Products, true);
xhttp.setRequestHeader("Authorization", public_token);
xhttp.send();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
var productInfo = xhttp.responseJSON;
Product = productInfo;
console.log(xhttp.responseText); // prints response in format of Search Products request in Ecwid API
}else{
console.log(xhttp.readyState + '<<State | Status>> '+xhttp.statusText);
}
};
// fetch(reqURL_Products, productGET)
// .then(response => response.json())
// .then(response => console.log(response))
// .then(Product = response)
// .catch(err => console.error(err));
//Gotta have that double redundancy to change pricing depending on the ID
if (Product.productID = productID){
switch(productID){
//For test site
case productID = '625285756':
Product.price = '185.00';
break;
case productID = '625238809':
Product.price = '77.00';
break;
case productID = '625270554': //Humidifier
Product.price = '1250';
break;
case productID = '625285757': //Dehumidifier
Product.price = '1650';
break;
// ////////////////////For Athena////////////////////
// case productID = '': //For Athena Pro Gro
// case Product.variant = '1':
// Product.price = '19.2';
// break;
// case Product.variant = '2':
// Product.price = '181.00';
// break;
// case productID = '': //For Athena Pro Core
// case Product.variant = '1':
// Product.price = '19.2';
// break;
// case Product.variant = '2':
// Product.price = '181.00';
// break;
// case productID = '': //For Athena Pro Bloom
// case Product.variant = '1':
// Product.price = '19.2';
// break;
// case Product.variant = '2':
// Product.price = '181.00';
// break;
// case productID = '': //For Athena CalMag
// case Product.variant = '1':
// Product.price = '36.46';
// break;
// case Product.variant = '2':
// Product.price = '162.00';
// break;
// case productID = '': //For Athena Cleanse
// case Product.variant = '1':
// Product.price = '60';
// break;
// case Product.variant = '2':
// Product.price = '243';
// break;
// case Product.variant = '3':
// Product.price = '2268.00'
// break;
// case productID = '': //For Athena Fade
// case Product.variant = '1':
// Product.price = '36.59';
// break;
// case Product.variant = '2':
// Product.price = '162'
// break;
// case Product.variant = '3':
// Product.price = '1498';
// break;
// ////////////////////For Jack's////////////////////
// case productID = '': //For Jack's Part A
// case Product.variant = '1':
// Product.price = '131.59'; //Variant 1 is 11kg variant for all of jacks
// break;
// case Product.variant = '2':
// Product.price = '29.86';
// break;
// case productID = '': //For Yara Calnit
// case Product.variant = '1':
// Product.price = '58.50';
// break;
// case Product.variant = '2':
// Product.price = '2.34';
// break;
// case Product.variant = '3':
// Product.price = '11.7';
// break
// case productID = '': //For Jack's Bloom
// case Product.variant = '1':
// Product.price = '165.24';
// break;
// case Product.variant = '2':
// Product.price = '33.61';
// break;
// case productID = '': //For Jack's RO
// case Product.variant = '1':
// Product.price = '120.41';
// break;
// case Product.variant = '2':
// Product.price = '33.61';
// break;
// case productID = '': //For Jack's Ultraviolet
// case Product.variant = '1':
// Product.price = '187.60';
// break;
// case Product.variant = '2':
// Product.price = '34.46';
// break;
// case productID = '': //For Jack's Finish
// case Product.variant = '1':
// Product.price = '137.202';
// break;
// case Product.variant = '2':
// Product.price = '30.78';
// break;
// case productID = '': //For Epsom Salts
// case Product.variant = '2':
// Product.price = '9.36';
// break;
// case Product.variant = '3':
// Product.price = '46.8';
// break;
}
var put = new XMLHttpRequest();
put.open("PUT", reqURL_Products, true);
put.setRequestHeader("Authorization", public_token);
put.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
var apiResponse = xhttp.responseText;
console.log(apiResponse); // prints response in format of Search Products request in Ecwid API
put.send(JSON.stringify(Product));
}else{
console.log(xhttp.readyState + '<<State | Status>> '+xhttp.statusText);
put.abort();
console.log('Aborted the saving of the product due to failed status');
}
};
}
//for loop to deactivate products & activate products ////////////////////If price changing fails
//Going to attempt to change price even though price is controlled by xSeries. Standby for that
}
}
function run(custGroupID){
console.log('Running RUN(), groupID is: ' + custGroupID);
custGroupID = JSON.stringify(custGroupID);
if(custGroupID = '23865254'){
console.log('RUNNING PRODUCTS');
runProducts();
// for(ids of cxGrId){
// if(customerInfo.customerGroup = '23865254'){
// //Get Products and replaces their pricing
// alert('RUNNING PRODUCTS');
// runProducts();
// }else{
// alert('RESETTING PRODUCTS');
// resetProducts();
// }
// }
}else if(custGroupID = '0'){
console.log('No Customer Group Found');
}else {
console.log('Something happened when getting customer groups');
}
}
window.Ecwid.OnPageLoad.add(function(page){ //For some reason grabbing the current customer is impossible.
var Cust = Ecwid.OnSetProfile.add(function(customer){
console.log('Member signed in');
console.log('data is: ' + JSON.stringify(customer));
return customer;
});
if (Cust.membership.id != 0){
run(membership.id);
}
});
// window.Ecwid.OnPageLoaded.add(function(page){
// alert('Page loaded: '+page.type);
// alert('About to run function run()');
// run();
// // if(page.type == 'CATEGORY'){
// // run();
// // }else if(page.type == 'PRODUCT'){
// // }else{
// // alert('ERROR ' + JSON.stringify(page.type));
// // }
// });