-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoxy-asecurecart.js
More file actions
302 lines (267 loc) · 9.97 KB
/
foxy-asecurecart.js
File metadata and controls
302 lines (267 loc) · 9.97 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
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
var FC = FC || {};
var foxySubdomain = foxySubdomain || "";
const existingOnLoadASecureCart = typeof FC.onLoad == "function" ? FC.onLoad : function () {};
FC.onLoad = function () {
existingOnLoadASecureCart();
FC.client.on("ready.done", () => {
const forms = document.querySelectorAll(
'form[action*="asecurecart.net"], form[action*=".foxycart.com"]'
);
// Map legacy input names to FoxyCart product option names
const inputNameMap = {
ID: "code",
Describe: "name",
describe: "name",
Price: "price",
Weight: "weight",
Qty: "quantity",
QtyMax: "quantity_max",
QtyMin: "quantity_min",
};
/**
* Logs debug info to console if ?debuga2f=1 is in the URL or global flag is set
* @param {string} msg - Message to log
* @param {...any} args - Additional arguments
*/
function debugLog(msg, ...args) {
const urlDebug = new URLSearchParams(window.location.search).get("debuga2f") === "1";
const isEnabled = window.__DEBUG_ASECURECART_TO_FOXY__ || urlDebug;
if (isEnabled) {
console.log("[A2F]", msg, ...args);
}
}
/**
* Parses a legacy AsecureCart value like "Option^Modifier" into parts
* @param {string} value - Raw value from input/select
* @returns {{display: string, modifier: string|null, isPercentage: boolean}}
*/
function parseModifier(value) {
const firstCaret = value.indexOf("^");
if (firstCaret === -1) return { display: value, modifier: null, isPercentage: false };
const displayText = value.substring(0, firstCaret).trim();
const rest = value.substring(firstCaret + 1).trim();
const modifierValue = rest.split("^")[0];
const isPercentage = modifierValue.endsWith("%");
debugLog("Parsed modifier", { displayText, modifierValue, isPercentage });
return { display: displayText, modifier: modifierValue, isPercentage };
}
/**
* Converts a numeric string into FoxyCart summing modifier syntax
* @param {string} mod - e.g. "2.00"
* @returns {string} - e.g. "{p+2.00}"
*/
function fixedModifierToFoxy(mod) {
return `{p+${mod}}`;
}
/**
* Converts a % modifier to fixed amount based on total
* @param {number} runningTotal - Current calculated product total
* @param {number} percentage - e.g. 15 for 15%
* @returns {string} - FoxyCart fixed modifier
*/
function percentageToFixedModifier(runningTotal, percentage) {
const fixedAmount = ((runningTotal * percentage) / 100).toFixed(2);
return `{p+${fixedAmount}}`;
}
/**
* Extracts number from a Foxy-style modifier, e.g. "{p+2.00}"
* @param {string} value
* @returns {number}
*/
function extractFixedModifier(value) {
// Look for a pattern like {p+NUMBER}
const match = value.match(/\{p\+([0-9.]+)\}/);
if (match && match[1]) {
return parseFloat(match[1]) || 0;
}
return 0;
}
/**
* Computes the total of all price-affecting inputs in the form
* @param {HTMLFormElement} form
* @returns {number}
*/
function calculateRunningTotal(form) {
const priceInput = form.querySelector('input[name="price"]');
let basePrice = priceInput ? parseFloat(priceInput.value) : 0;
if (isNaN(basePrice)) basePrice = 0;
let total = basePrice;
const elements = form.querySelectorAll("input, select");
for (let el of elements) {
let valueStr = "";
if (el.tagName.toLowerCase() === "select") {
const selected = el.options[el.selectedIndex];
if (!selected || selected.hasAttribute("data-is-percentage")) continue;
valueStr = selected.value;
} else if (el.tagName.toLowerCase() === "input") {
if ((el.type === "radio" || el.type === "checkbox") && !el.checked) continue;
if (el.hasAttribute("data-is-percentage")) continue;
valueStr = el.value;
}
if (!valueStr) continue;
const foxyMatch = valueStr.match(/\{p\+([0-9.]+)\}/);
if (foxyMatch) {
total += parseFloat(foxyMatch[1]) || 0;
continue;
}
if (valueStr.includes("^")) {
let parts = valueStr.split("^").map(s => s.trim());
if (parts.length < 2) continue;
let modifier = parts[1];
if (modifier.endsWith("%")) continue;
let fixed = parseFloat(modifier);
if (!isNaN(fixed)) total += fixed;
}
}
debugLog("Calculated running total:", total);
return total;
}
/**
* Updates percentage-based modifiers to fixed values based on total
* @param {HTMLFormElement} form
*/
function updatePercentageModifiers(form) {
const runningTotal = calculateRunningTotal(form);
form.querySelectorAll('[data-is-percentage="true"]').forEach(el => {
const percentage = parseFloat(el.dataset.percent);
if (isNaN(percentage)) return;
const displayText = el.dataset.display;
const newModifier = percentageToFixedModifier(runningTotal, percentage);
el.value = `${displayText}${newModifier}`;
debugLog("Updated percentage modifier", el.name, el.value);
});
}
/**
* Converts an AsecureCart-style "Recur" value to FoxyCart "sub_frequency"
* @param {string} value
* @returns {string}
*/
function transformRecurToSubFrequency(value) {
const parts = value.split("^");
let recurPart = parts[0].trim();
if (!recurPart) recurPart = "M1";
if (recurPart.length < 2) recurPart += "1";
let period = recurPart.charAt(0).toUpperCase();
let increment = recurPart.slice(1);
let num = parseFloat(increment);
if (isNaN(num)) num = 1;
if (period === "Q") {
num = num * 3;
period = "m";
} else {
period = period.toLowerCase();
}
return `${num}${period}`;
}
function processInputs(form) {
// Process all inputs and selects in the form
form.querySelectorAll("input").forEach(input => {
if (input.type === "submit") return;
if (input.name === "Qty") {
input.setAttribute("value", "1");
input.setAttribute("min", "1");
}
if (input.name === "ReturnLink" || input.name === "Returnlink") {
input.name = "url";
if (input.value.includes("^")) {
const splitReturnLink = input.value.split("^");
const productLink = splitReturnLink[0];
const productImageURL = splitReturnLink[1];
input.value = productLink;
const productImageInput = document.createElement("input");
productImageInput.type = "hidden";
productImageInput.name = "image";
productImageInput.value = productImageURL;
form.appendChild(productImageInput);
debugLog("Processed returnlink + image", productLink, productImageURL);
return;
}
}
if (input.name === "Recur" && input.value) {
input.name = "sub_frequency";
input.value = transformRecurToSubFrequency(input.value);
debugLog("Transformed Recur field to sub_frequency", input.value);
}
if (inputNameMap.hasOwnProperty(input.name)) {
input.name = inputNameMap[input.name];
}
const allowedOptionsRegex = /^(Size|Color|AddOn\d*|NAddOn\d*)/i;
if (input.value && input.value.includes("^") && allowedOptionsRegex.test(input.name)) {
const { display, modifier, isPercentage } = parseModifier(input.value);
if (!isPercentage && modifier) {
input.value = `${display}${fixedModifierToFoxy(modifier)}`;
debugLog("Applied fixed modifier to input", input.name, input.value);
} else if (isPercentage) {
input.dataset.isPercentage = "true";
input.dataset.percent = modifier.replace("%", "");
input.dataset.display = display;
input.value = display;
debugLog("Registered percentage modifier input", input.name, modifier);
}
}
});
}
function processSelects(form) {
// Process all selects in the form
form.querySelectorAll("select").forEach(select => {
if (select.name === "Recur") {
select.name = "sub_frequency";
select.querySelectorAll("option").forEach(option => {
if (option.value) option.value = transformRecurToSubFrequency(option.value);
});
}
select.querySelectorAll("option").forEach(option => {
const allowedOptionsRegex = /^(Size|Color|AddOn\d*|NAddOn\d*)/i;
if (
option.value &&
option.value.includes("^") &&
allowedOptionsRegex.test(select.name)
) {
const { display, modifier, isPercentage } = parseModifier(option.value);
if (!isPercentage && modifier) {
option.value = `${display}${fixedModifierToFoxy(modifier)}`;
debugLog("Applied fixed modifier to select option", select.name, option.value);
} else if (isPercentage) {
option.dataset.isPercentage = "true";
option.dataset.percent = modifier.replace("%", "");
option.dataset.display = display;
option.value = display;
debugLog("Registered percentage modifier select", select.name, modifier);
}
}
});
});
}
forms.forEach(form => {
form.action = foxySubdomain ? `${foxySubdomain}/cart` : `https://${FC?.settings?.storedomain}/cart` ;
processInputs(form);
processSelects(form)
if (!form.querySelector('input[name="name"]')) {
const codeInput = form.querySelector('input[name="code"]');
if (codeInput) {
const hiddenName = document.createElement("input");
hiddenName.type = "hidden";
hiddenName.name = "name";
hiddenName.value = codeInput.value;
form.appendChild(hiddenName);
debugLog("Added missing name input using code", codeInput.value);
}
}
if (!form.querySelector('input[name="price"]')) {
const hiddenPrice = document.createElement("input");
hiddenPrice.type = "hidden";
hiddenPrice.name = "price";
hiddenPrice.value = 0;
form.appendChild(hiddenPrice);
debugLog("Added missing price input with default value", hiddenPrice.value);
}
form.addEventListener("change", (e) => {
updatePercentageModifiers(form);
processInputs(form);
processSelects(form);
debugLog("Form changed", e.target.name, e.target.value);
});
updatePercentageModifiers(form);
});
});
}