-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuc_out_of_stock.js
178 lines (158 loc) · 7.1 KB
/
uc_out_of_stock.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
(function ($) {
Backdrop.behaviors.ucOutOfStock = {
attach: function(context) {
if(typeof console === "undefined") {
console = {log: function() {}};
};
// Your code here
attrid = 'edit-attributes';
function checkStock(forms) {
var form_ids = new Array();
var node_ids = new Array();
var attr_ids = new Array();
$.each(forms, function(index, form) {
var product = new Object();
var attributes = new Object();
var nid = form.attr('id').match(/(?:uc-product-add-to-cart-form-|catalog-buy-it-now-form-)([0-9]+)/)[1];
attributes.found = new Object();
attributes.value = new Object();
$("[name*=attributes]", form).filter(':input:not(:text):not(:checkbox)').each(function(index){
// We are assuming the value has to be there, seems to be working for radios and checkboxes
if ($(this).attr('name').match(/attributes\[([0-9]*)\]/)) {
id = $(this).attr('name').match(/attributes\[([0-9]*)\]/)[1];
if ($(this).is(':radio')) {
attributes.found['attr'+id] = 1;
if ($(this).is(':checked')) {
if ($(this).val()) {
attributes.value['attr'+id] = 1;
attr_ids.push(nid + ':' + id + ':' + $(this).val());
}
}
}
else if ($(this).is('select')) {
attributes.found['attr'+id] = 1;
if ($(this).val()) {
attributes.value['attr'+id] = 1;
attr_ids.push(nid + ':' + id + ':' + $(this).val());
}
}
}
});
// find qty
product['qty'] = 1;
qty = $(':input[name="qty"]', form).val()
if (qty) {
product['qty'] = qty;
}
// finding if attributes are found with no value
attributes.found.length = attributes.value.length = 0;
for (var i in attributes.found) {
if (i!='length') {
attributes.found.length++;
}
}
for (var i in attributes.value) {
if (i!='length') {
attributes.value.length++;
}
}
if (Backdrop.settings.uc_out_of_stock.throbber) {
$(".uc_out_of_stock_throbbing", form).addClass('uc_oos_throbbing');
}
form_ids.push(form.attr('id'));
node_ids.push(nid);
});
if (form_ids.length == 0) {
return;
}
var post = { 'form_ids[]': form_ids, 'node_ids[]': node_ids, 'attr_ids[]': attr_ids }
$.ajax({
type: 'post',
url : Backdrop.settings.uc_out_of_stock.path,
data: post,
success : function (data, textStatus) {
$.each(data, function(form_id, stock_level) {
var form = $('#' + form_id);
if (stock_level != null && parseInt(stock_level) <= 0) {
// hide add to cart button
$("input:submit.node-add-to-cart,input:submit.list-add-to-cart,button.node-add-to-cart,button.list-add-to-cart", form).hide();
// hide qty label, field and wrapper if present and if it follows
// default theme output
$("label[for=" + $(":input[name=qty]", form).attr('id') + "]", form).hide();
$(":input[name=qty]", form).hide();
$("#" + $(":input[name=qty]", form).attr('id') + "-wrapper", form).hide();
// Show out of stock message
$(".uc_out_of_stock_html", form).html(Backdrop.settings.uc_out_of_stock.msg);
if (Backdrop.settings.uc_out_of_stock.instock) {
$(".uc-out-of-stock-instock", form).hide();
}
}
else {
// Put back the normal HTML of the add to cart form
$(".uc_out_of_stock_html", form).html('');
// show qty elements
$("label[for=" + $(":input[name=qty]", form).attr('id') + "]", form).show();
$(":input[name=qty]", form).show();
$("#" + $(":input[name=qty]", form).attr('id') + "-wrapper", form).show();
// show add to cart button
$("input:submit.node-add-to-cart,input:submit.list-add-to-cart,button.node-add-to-cart,button.list-add-to-cart", form).show();
if (Backdrop.settings.uc_out_of_stock.instock) {
$(".uc-out-of-stock-instock", form).html(Backdrop.theme('ucOutOfStockInStock', stock_level));
$(".uc-out-of-stock-instock", form).show();
}
}
if (Backdrop.settings.uc_out_of_stock.throbber) {
$(".uc_out_of_stock_throbbing", form).removeClass('uc_oos_throbbing');
}
});
},
error : function (jqXHR, textStatus, errorThrown) {
console.log('uc_out_of_stock: ' + textStatus + ': ' + jqXHR.responseText);
if (Backdrop.settings.uc_out_of_stock.throbber) {
$(".uc_out_of_stock_throbbing").removeClass('uc_oos_throbbing');
}
},
dataType: 'json'
});
}
var forms = new Array();
$("form[id*=uc-product-add-to-cart-form], form[id*=uc-catalog-buy-it-now-form]").not('.uc-out-stock-processed').each(function() {
$(this).addClass('uc-out-stock-processed');
forms.push($(this));
if (Backdrop.settings.uc_out_of_stock.throbber) {
$("input:submit.node-add-to-cart,input:submit.list-add-to-cart,button.node-add-to-cart,button.list-add-to-cart", $(this)).before('<div class="uc_out_of_stock_throbbing"> </div>');
}
if (Backdrop.settings.uc_out_of_stock.instock) {
if ($(':input[name="qty"][type!="hidden"]', $(this)).length) {
$(":input[name=qty]", $(this)).after('<div class="uc-out-of-stock-instock"></div>');
}
else {
$("input:submit.node-add-to-cart,input:submit.list-add-to-cart,button.node-add-to-cart,button.list-add-to-cart", $(this)).before('<div class="uc-out-of-stock-instock"></div>');
}
}
$("input:submit.node-add-to-cart,input:submit.list-add-to-cart,button.node-add-to-cart,button.list-add-to-cart", $(this)).after('<div class="uc_out_of_stock_html"></div>');
var form = $(this);
$("[name*=attributes]", $(this)).filter(':input:not(:text):not(:checkbox)').change(function(){
checkStock([form]);
});
/* TODO: Feature request - support qty field, would make sense if cart
* contents are checked in the server as well as just stock
*/
/*
$(":input[name=qty]", $(this)).keyup(function(){
checkStock(eachForm);
});
*/
});
checkStock(forms);
}
}
Backdrop.theme.prototype.ucOutOfStockInStock = function (stock) {
if (stock == undefined) {
return Backdrop.t('In stock');
}
else {
return Backdrop.t('@stock in stock', {'@stock' : stock});
}
};
})(jQuery);