This repository was archived by the owner on May 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjquery.shiptime.js
More file actions
191 lines (159 loc) · 6.2 KB
/
jquery.shiptime.js
File metadata and controls
191 lines (159 loc) · 6.2 KB
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
/*!
* @package jquery.shiptime
* @version 0.1.3
* @copyright (C) 2014 Dane Grant ([email protected])
* @license MIT
*/
;(function($) {
var defaults = {
// UTC timezone offset for your stores location
timezone: '-0400',
// Enter your shipping cutoff deadline in 24 hour format
deadline: {
hour: 16,
minute: 0
},
// Name of estimated shipping method
shipMethodName: 'Fixed Rate Shipping',
// This is to make sure we only show transit info if an item is in stock!
stockSelector: '.availability span', // selector for element that notifies stock status
inStockText: 'In stock', // in stock text
// inStockText : true, // use this if you want to show for all items
// Add dates that we will be excluding from shipping and delivery (comma separated)
noShipping: '12/24,12/25',
// If you want to estimate delivery dates enter estimated lead time for each state in days
leadtime: {
'Alabama': 2,
'Alaska': 6,
'Arizona': 5,
'Arkansas': 2,
'California': 5,
'Colorado': 4,
'Connnecticut': 3,
'Delaware': 3,
'Florida': 1,
'Georgia': 2,
'Hawaii': 7,
'Idaho': 5,
'Illinois': 3,
'Indiana': 2,
'Iowa': 3,
'Kansas': 3,
'Kentucky': 2,
'Louisiana': 2,
'Maine': 3,
'Maryland': 3,
'Massachusetts': 3,
'Michigan': 3,
'Minnesota': 3,
'Mississippi': 2,
'Missouri': 3,
'Montana': 5,
'Nebraska': 4,
'Nevada': 5,
'New Hampshire': 3,
'New Jersey': 3,
'New Mexico': 4,
'New York': 4,
'North Carolina': 2,
'North Dakota': 5,
'Ohio': 2,
'Oklahoma': 3,
'Oregon': 5,
'Pennsylvania': 3,
'Rhode Island': 3,
'South Carolina': 2,
'South Dakota': 4,
'Tennessee': 2,
'Texas': 3,
'Utah': 5,
'Vermont': 3,
'Virginia': 2,
'Washington': 5,
'West Virginia': 2,
'Wisconsin': 3,
'Wyoming': 5
}
};
function ShippingTime(element, options) {
this.element = element;
this.options = $.extend({}, defaults, options);
try {
if (moment === undefined || $ === undefined ) throw Error("please include jquery & moment.js");
} catch (e) {
console.log(e);
}
// Display shipping deadline info
this.displayShipTime();
}
ShippingTime.prototype.isWeekday = function(moment) {
return (moment.day() !== 0 && moment.day() !== 6) ? true : false;
};
ShippingTime.prototype.isExcluded = function(moment) {
var dates = this.options.noShipping.split(',');
for(var i = 0; i < dates.length; i++) {
if (moment.zone("-0500").format("M/D") == dates[i]) {
return true;
}
}
return false;
};
ShippingTime.prototype.shipDay = function(deadline) {
return (moment().zone(this.options.timezone).isSame(deadline, "d")) ? "Today" : deadline.format("dddd");
};
ShippingTime.prototype.shippingDeadline = function(hour, minute) {
var deadline = moment().zone(this.options.timezone).hour(hour).minute(minute).second(0);
while (this.isExcluded(deadline)) {
deadline.add('h', 24);
}
if (moment().zone(this.options.timezone).hour() >= deadline.hour() && this.isWeekday(moment().zone(this.options.timezone))) {
deadline.add('h', 24);
}
if (!this.isWeekday(deadline)) {
return (deadline.day() === 0) ? deadline.add('h', 24) : deadline.add('h', 48);
} else {
return deadline;
}
};
ShippingTime.prototype.timeUntilDeadline = function(deadline) {
var minutesTill = Math.abs(moment().zone(this.options.timezone).diff(deadline, 'm'));
for (var hoursTill = 0; minutesTill >= 60; hoursTill++) {
minutesTill -= 60;
}
return { 'hours': hoursTill, 'minutes': minutesTill };
};
ShippingTime.prototype.projectedDeliveryDate = function(deliveryTime, state) {
var estimateDay = this.deliveryTime.add('d', this.options.leadtime[state]).format("dddd, MMM Do");
while (this.isExcluded(estimateDay)) {
estimateDay.add('h', 24);
}
if (!this.isWeekday(estimateDay)) {
return (estimateDay.day() === 0) ? estimateDay.add('h', 24) : estimateDay.add('h', 48);
} else {
return estimateDay;
}
};
ShippingTime.prototype.displayShipTime = function() {
var plugin = this;
if ($(this.options.stockSelector).text() == this.options.inStockText || this.options.inStockText === true) {
var deadline = this.shippingDeadline(this.options.deadline.hour, this.options.deadline.minute);
var timeLeft = this.timeUntilDeadline(deadline);
$(this.element).append('<span class="order">Order</span> within <span class="time-left">' + timeLeft.hours + ' hrs ' + timeLeft.minutes + ' mins</span> to ship <span class="ship-day">' + this.shipDay(deadline) + '</span>');
if (this.options.leadtime) {
$.get("http://getgeoip.net/json/", function (data) {
;
}, "jsonp")
.then( function(data) {
if (data.country_code == "US" && data.region_name !== null) {
$(plugin.element).append('<span class="est-delivery">Est Delivery:</span> ' + plugin.projectedDeliveryDate(deadline, data.region_name) + '<br><span class="deliver-time"> to ' + data.region_name + ' via ' + plugin.options.shipMethodName + '</span>');
}
});
}
}
};
$.fn.shipTime = function(options) {
return this.each(function() {
new ShippingTime(this, options);
});
};
})(jQuery);