This repository has been archived by the owner on Aug 30, 2018. It is now read-only.
/
ajax-cart.js.liquid
563 lines (477 loc) · 17 KB
/
ajax-cart.js.liquid
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*============================================================================
Ajax the add to cart experience by revealing it in a side drawer
Plugin Documentation - http://shopify.github.io/Timber/#ajax-cart
(c) Copyright 2015 Shopify Inc. Author: Carson Shold (@cshold). All Rights Reserved.
This file includes:
- Basic Shopify Ajax API calls
- Ajax cart plugin
This requires:
- jQuery 1.8+
- handlebars.min.js (for cart template)
- modernizr.min.js
- snippet/ajax-cart-template.liquid
Customized version of Shopify's jQuery API
(c) Copyright 2009-2015 Shopify Inc. Author: Caroline Schnapp. All Rights Reserved.
==============================================================================*/
if ((typeof ShopifyAPI) === 'undefined') { ShopifyAPI = {}; }
/*============================================================================
API Helper Functions
==============================================================================*/
function attributeToString(attribute) {
if ((typeof attribute) !== 'string') {
attribute += '';
if (attribute === 'undefined') {
attribute = '';
}
}
return jQuery.trim(attribute);
};
/*============================================================================
API Functions
==============================================================================*/
ShopifyAPI.onCartUpdate = function(cart) {
// alert('There are now ' + cart.item_count + ' items in the cart.');
};
ShopifyAPI.updateCartNote = function(note, callback) {
var $body = $(document.body),
params = {
type: 'POST',
url: '/cart/update.js',
data: 'note=' + attributeToString(note),
dataType: 'json',
beforeSend: function() {
$body.trigger('beforeUpdateCartNote.ajaxCart', note);
},
success: function(cart) {
if ((typeof callback) === 'function') {
callback(cart);
}
else {
ShopifyAPI.onCartUpdate(cart);
}
$body.trigger('afterUpdateCartNote.ajaxCart', [note, cart]);
},
error: function(XMLHttpRequest, textStatus) {
$body.trigger('errorUpdateCartNote.ajaxCart', [XMLHttpRequest, textStatus]);
ShopifyAPI.onError(XMLHttpRequest, textStatus);
},
complete: function(jqxhr, text) {
$body.trigger('completeUpdateCartNote.ajaxCart', [this, jqxhr, text]);
}
};
jQuery.ajax(params);
};
ShopifyAPI.onError = function(XMLHttpRequest, textStatus) {
var data = eval('(' + XMLHttpRequest.responseText + ')');
if (!!data.message) {
alert(data.message + '(' + data.status + '): ' + data.description);
}
};
/*============================================================================
POST to cart/add.js returns the JSON of the cart
- Allow use of form element instead of just id
- Allow custom error callback
==============================================================================*/
ShopifyAPI.addItemFromForm = function(form, callback, errorCallback) {
var $body = $(document.body),
params = {
type: 'POST',
url: '/cart/add.js',
data: jQuery(form).serialize(),
dataType: 'json',
beforeSend: function(jqxhr, settings) {
$body.trigger('beforeAddItem.ajaxCart', form);
},
success: function(line_item) {
if ((typeof callback) === 'function') {
callback(line_item, form);
}
else {
ShopifyAPI.onItemAdded(line_item, form);
}
$body.trigger('afterAddItem.ajaxCart', [line_item, form]);
},
error: function(XMLHttpRequest, textStatus) {
if ((typeof errorCallback) === 'function') {
errorCallback(XMLHttpRequest, textStatus);
}
else {
ShopifyAPI.onError(XMLHttpRequest, textStatus);
}
$body.trigger('errorAddItem.ajaxCart', [XMLHttpRequest, textStatus]);
},
complete: function(jqxhr, text) {
$body.trigger('completeAddItem.ajaxCart', [this, jqxhr, text]);
}
};
jQuery.ajax(params);
};
// Get from cart.js returns the cart in JSON
ShopifyAPI.getCart = function(callback) {
$(document.body).trigger('beforeGetCart.ajaxCart');
jQuery.getJSON('/cart.js', function (cart, textStatus) {
if ((typeof callback) === 'function') {
callback(cart);
}
else {
ShopifyAPI.onCartUpdate(cart);
}
$(document.body).trigger('afterGetCart.ajaxCart', cart);
});
};
// POST to cart/change.js returns the cart in JSON
ShopifyAPI.changeItem = function(line, quantity, callback) {
var $body = $(document.body),
params = {
type: 'POST',
url: '/cart/change.js',
data: 'quantity=' + quantity + '&line=' + line,
dataType: 'json',
beforeSend: function() {
$body.trigger('beforeChangeItem.ajaxCart', [line, quantity]);
},
success: function(cart) {
if ((typeof callback) === 'function') {
callback(cart);
}
else {
ShopifyAPI.onCartUpdate(cart);
}
$body.trigger('afterChangeItem.ajaxCart', [line, quantity, cart]);
},
error: function(XMLHttpRequest, textStatus) {
$body.trigger('errorChangeItem.ajaxCart', [XMLHttpRequest, textStatus]);
ShopifyAPI.onError(XMLHttpRequest, textStatus);
},
complete: function(jqxhr, text) {
$body.trigger('completeChangeItem.ajaxCart', [this, jqxhr, text]);
}
};
jQuery.ajax(params);
};
/*============================================================================
Ajax Shopify Add To Cart
==============================================================================*/
var ajaxCart = (function(module, $) {
'use strict';
// Public functions
var init, loadCart;
// Private general variables
var settings, isUpdating, $body;
// Private plugin variables
var $formContainer, $addToCart, $cartCountSelector, $cartCostSelector, $cartContainer, $drawerContainer;
// Private functions
var updateCountPrice, formOverride, itemAddedCallback, itemErrorCallback, cartUpdateCallback, buildCart, cartCallback, adjustCart, adjustCartCallback, createQtySelectors, qtySelectors, validateQty;
/*============================================================================
Initialise the plugin and define global options
==============================================================================*/
init = function (options) {
// Default settings
settings = {
formSelector : 'form[action^="/cart/add"]',
cartContainer : '#CartContainer',
addToCartSelector : 'input[type="submit"]',
cartCountSelector : null,
cartCostSelector : null,
moneyFormat : '${{amount}}',
disableAjaxCart : false,
enableQtySelectors : true
};
// Override defaults with arguments
$.extend(settings, options);
// Select DOM elements
$formContainer = $(settings.formSelector);
$cartContainer = $(settings.cartContainer);
$addToCart = $formContainer.find(settings.addToCartSelector);
$cartCountSelector = $(settings.cartCountSelector);
$cartCostSelector = $(settings.cartCostSelector);
// General Selectors
$body = $(document.body);
// Track cart activity status
isUpdating = false;
// Setup ajax quantity selectors on the any template if enableQtySelectors is true
if (settings.enableQtySelectors) {
qtySelectors();
}
// Take over the add to cart form submit action if ajax enabled
if (!settings.disableAjaxCart && $addToCart.length) {
formOverride();
}
// Run this function in case we're using the quantity selector outside of the cart
adjustCart();
};
loadCart = function () {
$body.addClass('drawer--is-loading');
ShopifyAPI.getCart(cartUpdateCallback);
};
updateCountPrice = function (cart) {
if ($cartCountSelector) {
$cartCountSelector.html(cart.item_count).removeClass('hidden-count');
if (cart.item_count === 0) {
$cartCountSelector.addClass('hidden-count');
}
}
if ($cartCostSelector) {
$cartCostSelector.html(Shopify.formatMoney(cart.total_price, settings.moneyFormat));
}
};
formOverride = function () {
$formContainer.on('submit', function(evt) {
evt.preventDefault();
// Add class to be styled if desired
$addToCart.removeClass('is-added').addClass('is-adding');
// Remove any previous quantity errors
$('.qty-error').remove();
ShopifyAPI.addItemFromForm(evt.target, itemAddedCallback, itemErrorCallback);
});
};
itemAddedCallback = function (product) {
$addToCart.removeClass('is-adding').addClass('is-added');
ShopifyAPI.getCart(cartUpdateCallback);
};
itemErrorCallback = function (XMLHttpRequest, textStatus) {
var data = eval('(' + XMLHttpRequest.responseText + ')');
$addToCart.removeClass('is-adding is-added');
if (!!data.message) {
if (data.status == 422) {
$formContainer.after('<div class="errors qty-error">'+ data.description +'</div>')
}
}
};
cartUpdateCallback = function (cart) {
// Update quantity and price
updateCountPrice(cart);
buildCart(cart);
};
buildCart = function (cart) {
// Start with a fresh cart div
$cartContainer.empty();
// Show empty cart
if (cart.item_count === 0) {
$cartContainer
.append('<p>' + {{ 'cart.general.empty' | t | json }} + '</p>');
cartCallback(cart);
return;
}
// Handlebars.js cart layout
var items = [],
item = {},
data = {},
source = $("#CartTemplate").html(),
template = Handlebars.compile(source);
// Add each item to our handlebars.js data
$.each(cart.items, function(index, cartItem) {
/* Hack to get product image thumbnail
* - If image is not null
* - Remove file extension, add _small, and re-add extension
* - Create server relative link
* - A hard-coded url of no-image
*/
if (cartItem.image != null){
var prodImg = cartItem.image.replace(/(\.[^.]*)$/, "_small$1").replace('http:', '');
} else {
var prodImg = "//cdn.shopify.com/s/assets/admin/no-image-medium-cc9732cb976dd349a0df1d39816fbcc7.gif";
}
// Create item's data object and add to 'items' array
item = {
key: cartItem.key,
line: index + 1, // Shopify uses a 1+ index in the API
url: cartItem.url,
img: prodImg,
name: cartItem.product_title,
variation: cartItem.variant_title,
properties: cartItem.properties,
itemAdd: cartItem.quantity + 1,
itemMinus: cartItem.quantity - 1,
itemQty: cartItem.quantity,
price: Shopify.formatMoney(cartItem.price, settings.moneyFormat),
vendor: cartItem.vendor,
linePrice: Shopify.formatMoney(cartItem.line_price, settings.moneyFormat),
originalLinePrice: Shopify.formatMoney(cartItem.original_line_price, settings.moneyFormat),
discounts: cartItem.discounts,
discountsApplied: cartItem.line_price === cartItem.original_line_price ? false : true
};
items.push(item);
});
// Gather all cart data and add to DOM
data = {
items: items,
note: cart.note,
totalPrice: Shopify.formatMoney(cart.total_price, settings.moneyFormat),
totalCartDiscount: cart.total_discount === 0 ? 0 : {{ 'cart.general.savings_html' | t: price: '[savings]' | json }}.replace('[savings]', Shopify.formatMoney(cart.total_discount, settings.moneyFormat)),
totalCartDiscountApplied: cart.total_discount === 0 ? false : true
}
$cartContainer.append(template(data));
cartCallback(cart);
};
cartCallback = function(cart) {
$body.removeClass('drawer--is-loading');
$body.trigger('afterCartLoad.ajaxCart', cart);
if (window.Shopify && Shopify.StorefrontExpressButtons) {
Shopify.StorefrontExpressButtons.initialize();
}
};
adjustCart = function () {
// Delegate all events because elements reload with the cart
// Add or remove from the quantity
$body.on('click', '.ajaxcart__qty-adjust', function() {
if (isUpdating) {
return;
}
var $el = $(this),
line = $el.data('line'),
$qtySelector = $el.siblings('.ajaxcart__qty-num'),
qty = parseInt($qtySelector.val().replace(/\D/g, ''));
var qty = validateQty(qty);
// Add or subtract from the current quantity
if ($el.hasClass('ajaxcart__qty--plus')) {
qty += 1;
} else {
qty -= 1;
if (qty <= 0) qty = 0;
}
// If it has a data-line, update the cart.
// Otherwise, just update the input's number
if (line) {
updateQuantity(line, qty);
} else {
$qtySelector.val(qty);
}
});
// Update quantity based on input on change
$body.on('change', '.ajaxcart__qty-num', function() {
if (isUpdating) {
return;
}
var $el = $(this),
line = $el.data('line'),
qty = parseInt($el.val().replace(/\D/g, ''));
var qty = validateQty(qty);
// If it has a data-line, update the cart
if (line) {
updateQuantity(line, qty);
}
});
// Prevent cart from being submitted while quantities are changing
$body.on('submit', 'form.ajaxcart', function(evt) {
if (isUpdating) {
evt.preventDefault();
}
});
// Highlight the text when focused
$body.on('focus', '.ajaxcart__qty-adjust', function() {
var $el = $(this);
setTimeout(function() {
$el.select();
}, 50);
});
function updateQuantity(line, qty) {
isUpdating = true;
// Add activity classes when changing cart quantities
var $row = $('.ajaxcart__row[data-line="' + line + '"]').addClass('is-loading');
if (qty === 0) {
$row.parent().addClass('is-removed');
}
// Slight delay to make sure removed animation is done
setTimeout(function() {
ShopifyAPI.changeItem(line, qty, adjustCartCallback);
}, 250);
}
// Save note anytime it's changed
$body.on('change', 'textarea[name="note"]', function() {
var newNote = $(this).val();
// Update the cart note in case they don't click update/checkout
ShopifyAPI.updateCartNote(newNote, function(cart) {});
});
};
adjustCartCallback = function (cart) {
// Update quantity and price
updateCountPrice(cart);
// Reprint cart on short timeout so you don't see the content being removed
setTimeout(function() {
isUpdating = false;
ShopifyAPI.getCart(buildCart);
}, 150)
};
createQtySelectors = function() {
// If there is a normal quantity number field in the ajax cart, replace it with our version
if ($('input[type="number"]', $cartContainer).length) {
$('input[type="number"]', $cartContainer).each(function() {
var $el = $(this),
currentQty = $el.val();
var itemAdd = currentQty + 1,
itemMinus = currentQty - 1,
itemQty = currentQty;
var source = $("#AjaxQty").html(),
template = Handlebars.compile(source),
data = {
key: $el.data('id'),
itemQty: itemQty,
itemAdd: itemAdd,
itemMinus: itemMinus
};
// Append new quantity selector then remove original
$el.after(template(data)).remove();
});
}
};
qtySelectors = function() {
// Change number inputs to JS ones, similar to ajax cart but without API integration.
// Make sure to add the existing name and id to the new input element
var numInputs = $('input[type="number"]');
if (numInputs.length) {
numInputs.each(function() {
var $el = $(this),
currentQty = $el.val(),
inputName = $el.attr('name'),
inputId = $el.attr('id');
var itemAdd = currentQty + 1,
itemMinus = currentQty - 1,
itemQty = currentQty;
var source = $("#JsQty").html(),
template = Handlebars.compile(source),
data = {
key: $el.data('id'),
itemQty: itemQty,
itemAdd: itemAdd,
itemMinus: itemMinus,
inputName: inputName,
inputId: inputId
};
// Append new quantity selector then remove original
$el.after(template(data)).remove();
});
// Setup listeners to add/subtract from the input
$('.js-qty__adjust').on('click', function() {
var $el = $(this),
id = $el.data('id'),
$qtySelector = $el.siblings('.js-qty__num'),
qty = parseInt($qtySelector.val().replace(/\D/g, ''));
var qty = validateQty(qty);
// Add or subtract from the current quantity
if ($el.hasClass('js-qty__adjust--plus')) {
qty += 1;
} else {
qty -= 1;
if (qty <= 1) qty = 1;
}
// Update the input's number
$qtySelector.val(qty);
});
}
};
validateQty = function (qty) {
if((parseFloat(qty) == parseInt(qty)) && !isNaN(qty)) {
// We have a valid number!
} else {
// Not a number. Default to 1.
qty = 1;
}
return qty;
};
module = {
init: init,
load: loadCart
};
return module;
}(ajaxCart || {}, jQuery));