-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathcart.js
363 lines (318 loc) · 9.16 KB
/
cart.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import BodlEventsCart from '../bodl/emitters/cart';
import Base from './base';
import Hooks from '../hooks';
export default class extends Base {
constructor(version) {
super(version);
this.bodlEventsCart = null;
}
getBodlEventsCart() {
if (!this.bodlEventsCart) {
this.bodlEventsCart = new BodlEventsCart();
}
return this.bodlEventsCart;
}
/**
* Get a collection of Carts. For now, this will only return an array of a single cart as multiple carts per session
* are not currently supported.
*
* @param options
* @param {Function} callback
*/
getCarts(options = {}, callback) {
let url = '/api/storefront/carts';
if (options.includeOptions) {
url = this.includeOptions(url);
}
this.makeRequest(url, 'GET', options, true, (err, response) => {
callback(err, response);
});
}
/**
* Get the current Cart's details, either with or without Product Option selections.
* Can also be used to get a particular cart provided a cartId in the options.
*
* @param options
* @param {Function} callback
*/
getCart(options = {}, callback) {
/* If no cart ID is provided, get the collection of carts and return the first one */
if (!options.cartId) {
return this.getCarts(options, (err, response) => callback(err, response[0]));
}
let url = `/api/storefront/carts/${options.cartId}`;
if (options.includeOptions) {
url = this.includeOptions(url);
}
this.makeRequest(url, 'GET', options, true, (err, response) => {
callback(err, response);
});
}
/**
* Get the summary for the current primary cart. This summary does not include the full details of the cart, but
* the response time is much faster and it's appropriate for summary use cases.
*
* @param options
* @param {Function} callback
*/
getCartSummary(options = {}, callback) {
const url = '/api/storefront/cart-summary';
this.makeRequest(url, 'GET', options, true, (err, response) => {
callback(err, response);
});
}
/**
* Add the parameters to a URL needed to get product option details on cart line items
* @param url
*/
includeOptions(url) {
return `${url}?include=lineItems.physicalItems.options,lineItems.digitalItems.options`;
}
/**
* Get a sum of the cart line item quantities
*
* @param options
* @param {Function} callback
*/
getCartQuantity(options = {}, callback) {
this.getCartSummary(options, (err, response) => {
if (err) {
return callback(err);
}
let quantity = 0;
if (response
&& response.status !== 204
&& response.total_quantity) {
quantity = response.total_quantity;
}
callback(null, quantity);
});
}
/**
* Enhance with Big Open Data Layer events
*
* @param {FormData} formData
* @param {Function} callback
*/
itemAdd(formData, callback) {
this.handleItemAdd(formData, (err, response) => {
if (!err) {
this.getBodlEventsCart().emitAddItem(response);
}
callback(err, response);
});
}
/**
* Add item to cart with options (variants)
*
* @param {FormData} formData
* @param {Function} callback
*/
handleItemAdd(formData, callback) {
this.remoteRequest('/cart/add', 'POST', { formData }, (err, response) => {
const emitData = {
err,
response,
};
Hooks.emit('cart-item-add-remote', emitData);
callback(err, response);
});
}
/**
* Update cart item quantity
*
* @param {String|Object} itemId
* @param {Number|Function} qty
* @param {Function|null} callback
*/
itemUpdate(itemId, qty, callback) {
let callbackArg = callback;
let items;
if (Array.isArray(itemId) && typeof qty === 'function') {
callbackArg = qty;
items = itemId;
} else {
items = [
{
id: itemId,
quantity: qty,
},
];
}
this.update(items, (err, response) => {
const emitData = {
items,
err,
response,
};
if (qty === 0 && !err) {
this.getBodlEventsCart().emitRemoveItem(response);
}
Hooks.emit('cart-item-update-remote', emitData);
callbackArg(err, response);
});
}
/**
* Enhance with Big Open Data Layer events
*
* @param {String} itemId
* @param {Function} callback
*/
itemRemove(itemId, callback) {
this.handleItemRemove(itemId, (err, response) => {
if (!err) {
this.getBodlEventsCart().emitRemoveItem(response);
}
callback(err, response);
});
}
/**
* Remove cart items
*
* Calls the internal update function with quantity: 0
*
* @param {String} itemId
* @param {Function} callback
*/
handleItemRemove(itemId, callback) {
const items = [
{
id: itemId,
quantity: 0,
},
];
this.update(items, (err, response) => {
const emitData = {
items,
err,
response,
};
Hooks.emit('cart-item-remove-remote', emitData);
callback(err, response);
});
}
/**
* Get giftwrapping options
* @param {String} itemId
* @param {Object|Function} options
* @param {Function|null} callback
*/
getItemGiftWrappingOptions(itemId, options, callback) {
let opts = options || {};
let callbackArg = callback;
if (typeof opts === 'function') {
callbackArg = opts;
opts = {};
}
this.remoteRequest(`/gift-wrapping/${itemId}`, 'GET', opts, callbackArg);
}
/**
* Submit giftwrapping options
*
* @param {String} itemId
* @param {Function} callback
*/
submitItemGiftWrappingOption(itemId, params, callback) {
this.remoteRequest(`/gift-wrapping/${itemId}`, 'POST', { params }, callback);
}
/**
* Update cart items
*
* @param {Array} items
* @param {Function} callback
*/
update(items, callback) {
const payload = {
items,
};
this.remoteRequest('/cart/update', 'POST', { params: payload }, callback);
}
/**
* POST form data to /cart.php
*
* @param {FormData} formData
* @param {Function} callback
*/
postFormData(formData, callback) {
this.makeRequest('/cart.php', 'POST', { formData }, false, callback);
}
/**
* Get cart content
*
* @param {Object} options
* @param {Function} callback
*/
getContent(options, callback) {
let opts = options || {};
let callbackArg = callback;
if (typeof opts === 'function') {
callbackArg = opts;
opts = {};
}
this.makeRequest('/cart.php', 'GET', opts, false, callbackArg);
}
/**
* Get cart shipping quote
*
* @param {Object} params
* @param {String|Array|Object} renderWith
* @param {Function} callback
*/
getShippingQuotes(params, renderWith, callback) {
const options = {
params,
};
let callbackArg = callback;
let renderWithArg = renderWith;
if (typeof callbackArg !== 'function') {
callbackArg = renderWithArg;
renderWithArg = null;
}
if (renderWithArg) {
options.template = renderWithArg;
}
this.remoteRequest('/shipping-quote', 'GET', options, callbackArg);
}
/**
* Submit shipping quote based on quoteId
*
* @param {Number} quoteId
* @param {Function} callback
*/
submitShippingQuote(quoteId, callback) {
const options = {
params: {
shipping_method: quoteId,
},
};
this.remoteRequest('/shipping-quote', 'POST', options, callback);
}
/**
* Apply a coupon code or gift certificate to the cart
*
* @param {String} code
* @param {Function} callback
*/
applyCode(code, callback) {
const options = {
params: {
code,
},
};
this.remoteRequest('/apply-code', 'POST', options, callback);
}
/**
* Apply a coupon code or gift certificate to the cart
*
* @param {Number} code
* @param {Function} callback
*/
applyGiftCertificate(code, callback) {
const options = {
params: {
code,
},
};
this.remoteRequest('/gift-certificates', 'POST', options, callback);
}
}