-
Notifications
You must be signed in to change notification settings - Fork 42
/
jquery.cart.js
239 lines (237 loc) · 9 KB
/
jquery.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
/**
* Module to add a shipping rates calculator to cart page.
*
* Copyright (c) 2011-2016 Caroline Schnapp (11heavens.com)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
// Adding utility function to Countries object defined in countries.js.
// Some countries use 'province' while others use 'state'.
if (typeof Countries === 'object') {
Countries.updateProvinceLabel = function(country, provinceLabelElement) {
if (typeof country === 'string' && Countries[country] && Countries[country].provinces) {
if (typeof provinceLabelElement !== 'object') {
provinceLabelElement = document.getElementById('address_province_label');
if (provinceLabelElement === null) return;
}
provinceLabelElement.innerHTML = Countries[country].label;
var provinceContainer = jQuery(provinceLabelElement).parent();
var provinceSelect = provinceContainer.find('select');
provinceContainer.find('.custom-style-select-box-inner').html(Countries[country].provinces[0]);
}
};
}
// Adding Shopify.Cart.ShippingCalculator object.
if (typeof Shopify.Cart === 'undefined') {
Shopify.Cart = {}
}
// Creating a module to encapsulate this.
Shopify.Cart.ShippingCalculator = (function() {
var _config = {
submitButton: 'Calculate shipping',
submitButtonDisabled: 'Calculating...',
templateId: 'shipping-calculator-response-template',
wrapperId: 'wrapper-response',
customerIsLoggedIn: false,
moneyFormat: '${{amount}}'
};
var _render = function(response) {
var template = jQuery('#' + _config.templateId);
var wrapper = jQuery('#' + _config.wrapperId);
if (template.length && wrapper.length) {
_.templateSettings = {
evaluate: /<%([\s\S]+?)%>/g,
interpolate: /<%=([\s\S]+?)%>/g,
escape: /<%-([\s\S]+?)%>/g
};
var myTemplate = _.template(jQuery.trim(template.text()));
var compiled = myTemplate(response);
jQuery(compiled).appendTo(wrapper);
if (typeof Currency !== 'undefined' && typeof Currency.convertAll === 'function') {
var newCurrency = '';
if (jQuery('[name=currencies]').size()) {
newCurrency = jQuery('[name=currencies]').val();
}
else if (jQuery('#currencies span.selected').size()) {
newCurrency = jQuery('#currencies span.selected').attr('data-currency');
}
if (newCurrency !== '') {
Currency.convertAll(shopCurrency, newCurrency, '#wrapper-response span.money, #estimated-shipping span.money');
}
}
}
};
var _enableButtons = function() {
jQuery('.get-rates').removeAttr('disabled').removeClass('disabled').val(_config.submitButton);
};
var _disableButtons = function() {
jQuery('.get-rates').val(_config.submitButtonDisabled).attr('disabled','disabled').addClass('disabled');
};
var _getCartShippingRatesForDestination = function(shippingAddress) {
var params = {
type: 'POST',
url: '/cart/prepare_shipping_rates',
data: jQuery.param({'shipping_address': shippingAddress}),
success: _pollForCartShippingRatesForDestination(shippingAddress),
error: _onError
}
jQuery.ajax(params);
};
var _pollForCartShippingRatesForDestination = function(shippingAddress) {
var poller = function() {
jQuery.ajax('/cart/async_shipping_rates', {
dataType: 'json',
success: function(response, textStatus, xhr) {
if (xhr.status === 200) {
_onCartShippingRatesUpdate(response.shipping_rates, shippingAddress)
} else {
setTimeout(poller, 500)
}
},
error: _onError
})
}
return poller;
};
var _fullMessagesFromErrors = function(errors) {
var fullMessages = [];
jQuery.each(errors, function(attribute, messages) {
jQuery.each(messages, function(index, message) {
fullMessages.push(attribute + ' ' + message);
});
});
return fullMessages;
};
var _onError = function(XMLHttpRequest, textStatus) {
jQuery('#estimated-shipping').hide();
jQuery('#estimated-shipping em').empty();
// Re-enable calculate shipping buttons.
_enableButtons();
// Formatting error message.
var feedback = '';
var data = eval('(' + XMLHttpRequest.responseText + ')');
if (!!data.message) {
feedback = data.message + '(' + data.status + '): ' + data.description;
}
else {
feedback = 'Error : ' + _fullMessagesFromErrors(data).join('; ') + '.';
}
if (feedback === 'Error : country is not supported.') feedback = 'We do not ship to this destination.';
// Update calculator.
_render( { rates: [], errorFeedback: feedback, success: false } );
jQuery('#' + _config.wrapperId).show();
};
var _onCartShippingRatesUpdate = function(rates, shipping_address) {
// Re-enable calculate shipping buttons.
_enableButtons();
// Formatting shipping address.
var readable_address = '';
if (shipping_address.zip) readable_address += shipping_address.zip + ', ';
if (shipping_address.province) readable_address += shipping_address.province + ', ';
readable_address += shipping_address.country;
// Show estimated shipping.
if (rates.length) {
if (rates[0].price == '0.00') {
jQuery('#estimated-shipping em').html('FREE');
}
else {
jQuery('#estimated-shipping em').html(_formatRate(rates[0].price));
}
for (var i=0; i<rates.length; i++) {
rates[i].price = _formatRate(rates[i].price);
}
}
// Show rates and feedback.
_render( { rates: rates, address: readable_address, success:true } );
// Revealing response.
jQuery('#' + _config.wrapperId + ', #estimated-shipping').fadeIn();
};
var _formatRate = function(cents) {
if (typeof Shopify.formatMoney === 'function') {
return Shopify.formatMoney(cents, _config.moneyFormat);
}
if (typeof cents == 'string') { cents = cents.replace('.',''); }
var value = '';
var placeholderRegex = /\{\{\s*(\w+)\s*\}\}/;
var formatString = _config.moneyFormat;
function defaultOption(opt, def) {
return (typeof opt == 'undefined' ? def : opt);
}
function formatWithDelimiters(number, precision, thousands, decimal) {
precision = defaultOption(precision, 2);
thousands = defaultOption(thousands, ',');
decimal = defaultOption(decimal, '.');
if (isNaN(number) || number == null) { return 0; }
number = (number/100.0).toFixed(precision);
var parts = number.split('.'),
dollars = parts[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1' + thousands),
cents = parts[1] ? (decimal + parts[1]) : '';
return dollars + cents;
}
switch(formatString.match(placeholderRegex)[1]) {
case 'amount':
value = formatWithDelimiters(cents, 2);
break;
case 'amount_no_decimals':
value = formatWithDelimiters(cents, 0);
break;
case 'amount_with_comma_separator':
value = formatWithDelimiters(cents, 2, '.', ',');
break;
case 'amount_no_decimals_with_comma_separator':
value = formatWithDelimiters(cents, 0, '.', ',');
break;
}
return formatString.replace(placeholderRegex, value);
};
_init = function() {
// Initialize observer on shipping address.
new Shopify.CountryProvinceSelector('address_country', 'address_province', { hideElement: 'address_province_container' } );
// Updating province label.
var countriesSelect = jQuery('#address_country');
var addressProvinceLabelEl = jQuery('#address_province_label').get(0);
if (typeof Countries !== 'undefined') {
Countries.updateProvinceLabel(countriesSelect.val(),addressProvinceLabelEl);
countriesSelect.change(function() {
Countries.updateProvinceLabel(countriesSelect.val(),addressProvinceLabelEl);
});
}
// When either of the calculator buttons is clicked, get rates.
jQuery('.get-rates').click(function() {
// Disabling all buttons.
_disableButtons();
// Hiding response.
jQuery('#' + _config.wrapperId).empty().hide();
// Reading shipping address for submission.
var shippingAddress = {};
shippingAddress.zip = jQuery('#address_zip').val() || '';
shippingAddress.country = jQuery('#address_country').val() || '';
shippingAddress.province = jQuery('#address_province').val() || '';
_getCartShippingRatesForDestination(shippingAddress);
});
// We don't wait for customer to click if we know his/her address.
if (_config.customerIsLoggedIn) {
jQuery('.get-rates:eq(0)').trigger('click');
}
};
return {
show: function(params) {
// Configuration
params = params || {};
// Merging with defaults.
jQuery.extend(_config, params);
// Action
jQuery(function() {
_init();
});
},
getConfig: function() {
return _config;
},
formatRate: function(cents) {
return _formatRate(cents);
}
}
})();