Skip to content

Commit 39cf044

Browse files
committed
Unify price extraction and update logic for box options
Refactored code in creatButtons.js and productPage.js to consistently use double quotes and improved logic for extracting and updating prices from option elements. Enhanced handling of nested box parameters and ensured price updates reflect the correct values in the UI.
1 parent 91e1e61 commit 39cf044

File tree

2 files changed

+73
-37
lines changed

2 files changed

+73
-37
lines changed

assets/js/components/creatButtons.js

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,17 @@ export function createOptions(position, orders) {
221221
if (isBoxParam) {
222222
// Determine box base price from the first option that has a surcharge final price
223223
const $select = $(position);
224-
const $firstWithPrice = $select.find('option[data-surcharge-final-price]:not([value=""])').filter(function () {
225-
const raw = String($(this).attr('data-surcharge-final-price') || $(this).attr('data-surcharge-additional-price') || '0');
226-
const num = Number(raw.replace(/[^0-9]/g, ''));
227-
return num > 0;
228-
}).first();
224+
const $firstWithPrice = $select
225+
.find('option[data-surcharge-final-price]:not([value=""])')
226+
.filter(function () {
227+
const raw = String($(this).attr("data-surcharge-final-price") || $(this).attr("data-surcharge-additional-price") || "0");
228+
const num = Number(raw.replace(/[^0-9]/g, ""));
229+
return num > 0;
230+
})
231+
.first();
229232
if ($firstWithPrice.length) {
230-
const raw = String($firstWithPrice.attr('data-surcharge-final-price') || $firstWithPrice.attr('data-surcharge-additional-price') || '0');
231-
basePrice = Number(raw.replace(/[^0-9]/g, ''));
233+
const raw = String($firstWithPrice.attr("data-surcharge-final-price") || $firstWithPrice.attr("data-surcharge-additional-price") || "0");
234+
basePrice = Number(raw.replace(/[^0-9]/g, ""));
232235
}
233236

234237
$('<span class="text">Cena boxu</span>').appendTo(priceWrap);
@@ -266,7 +269,22 @@ export function createOptions(position, orders) {
266269
class: "option-wrap",
267270
}).appendTo(parametrWraps);
268271
const options = $(this).find("option");
269-
createOptionButtons(options, parameterId, optionWrap, false, 0);
272+
// determine if nested param is a box param and compute its base price
273+
const isBoxNested = boxsParameterIds.includes(parseInt(parameterId));
274+
let basePriceNested = 0;
275+
if (isBoxNested) {
276+
const $select = $(this).find('select');
277+
const $firstWithPrice = $select.find('option[data-surcharge-final-price]:not([value=""])').filter(function () {
278+
const raw = String($(this).attr('data-surcharge-final-price') || $(this).attr('data-surcharge-additional-price') || '0');
279+
const num = Number(raw.replace(/[^0-9]/g, ''));
280+
return num > 0;
281+
}).first();
282+
if ($firstWithPrice.length) {
283+
const raw = String($firstWithPrice.attr('data-surcharge-final-price') || $firstWithPrice.attr('data-surcharge-additional-price') || '0');
284+
basePriceNested = Number(raw.replace(/[^0-9]/g, ''));
285+
}
286+
}
287+
createOptionButtons(options, parameterId, optionWrap, isBoxNested, basePriceNested);
270288
});
271289

272290
$(".parameter-wrap.parameter-sizes").eq(2).hide();
@@ -431,14 +449,14 @@ function createOptionButtons(options, parameterId, optionsWrap, isBoxParam = fal
431449
$(optionButton).addClass("text");
432450
// If this is a box parameter, clicking option updates the price-standart for this parameter
433451
if (isBoxParam) {
434-
$(optionButton).on('click', function () {
435-
$(this).addClass('active').siblings().removeClass('active');
436-
const finalRaw = String($opt.attr('data-surcharge-final-price') || $opt.attr('data-surcharge-additional-price') || '0');
437-
const finalPrice = Number(finalRaw.replace(/[^0-9]/g, ''));
438-
const $priceEl = $(this).closest('.parameter-wrap').find('.price.price-standart');
452+
$(optionButton).on("click", function () {
453+
$(this).addClass("active").siblings().removeClass("active");
454+
const finalRaw = String($opt.attr("data-surcharge-final-price") || $opt.attr("data-surcharge-additional-price") || "0");
455+
const finalPrice = Number(finalRaw.replace(/[^0-9]/g, ""));
456+
const $priceEl = $(this).closest(".parameter-wrap").find(".price.price-standart");
439457
if ($priceEl.length) {
440-
$priceEl.attr('data-price', finalPrice);
441-
$priceEl.text(finalPrice > 0 ? NumToPrice(finalPrice) : '0 Kč');
458+
$priceEl.attr("data-price", finalPrice);
459+
$priceEl.text(finalPrice > 0 ? NumToPrice(finalPrice) : "0 Kč");
442460
}
443461
});
444462
}

assets/js/components/productPage.js

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,22 +1057,31 @@ function updateUpsale($this, event) {
10571057
const $soloSelect = $(`select.parameter-id-${soloId}.surcharge-parameter`);
10581058
let soloPrice = 0;
10591059
if ($soloSelect.length) {
1060-
const $sel = $soloSelect.find('option:selected');
1061-
const raw = String($sel.attr('data-surcharge-final-price') || $sel.attr('data-surcharge-additional-price') || '');
1062-
if (raw && raw.replace(/[^0-9]/g, '') !== '') {
1063-
soloPrice = Number(raw.replace(/[^0-9]/g, ''));
1060+
const $sel = $soloSelect.find("option:selected");
1061+
const raw = String($sel.attr("data-surcharge-final-price") || $sel.attr("data-surcharge-additional-price") || "");
1062+
if (raw && raw.replace(/[^0-9]/g, "") !== "") {
1063+
soloPrice = Number(raw.replace(/[^0-9]/g, ""));
10641064
} else {
1065-
const $first = $soloSelect.find('option[data-surcharge-final-price]:not([value=""])').filter(function () {
1066-
return Number(String($(this).attr('data-surcharge-final-price') || $(this).attr('data-surcharge-additional-price') || '0').replace(/[^0-9]/g, '')) > 0;
1067-
}).first();
1065+
const $first = $soloSelect
1066+
.find('option[data-surcharge-final-price]:not([value=""])')
1067+
.filter(function () {
1068+
return (
1069+
Number(
1070+
String($(this).attr("data-surcharge-final-price") || $(this).attr("data-surcharge-additional-price") || "0").replace(/[^0-9]/g, ""),
1071+
) > 0
1072+
);
1073+
})
1074+
.first();
10681075
if ($first.length) {
1069-
soloPrice = Number(String($first.attr('data-surcharge-final-price') || $first.attr('data-surcharge-additional-price') || '0').replace(/[^0-9]/g, ''));
1076+
soloPrice = Number(
1077+
String($first.attr("data-surcharge-final-price") || $first.attr("data-surcharge-additional-price") || "0").replace(/[^0-9]/g, ""),
1078+
);
10701079
}
10711080
}
10721081
}
1073-
const $soloPriceEl = $(".parameter-wrap.parameter-" + soloId).find('.price.price-standart');
1074-
$soloPriceEl.attr('data-price', soloPrice);
1075-
if ($soloPriceEl.length) $soloPriceEl.text(soloPrice > 0 ? NumToPrice(soloPrice) : '0 Kč');
1082+
const $soloPriceEl = $(".parameter-wrap.parameter-" + soloId).find(".price.price-standart");
1083+
$soloPriceEl.attr("data-price", soloPrice);
1084+
if ($soloPriceEl.length) $soloPriceEl.text(soloPrice > 0 ? NumToPrice(soloPrice) : "0 Kč");
10761085
} else if (value[0] === "conf2") {
10771086
// conf2: hide Solo box, show other box parameters (box1/box2)
10781087
const soloId = 78;
@@ -1084,22 +1093,31 @@ function updateUpsale($this, event) {
10841093
const $box1Select = $(`select.parameter-id-${box1}.surcharge-parameter`);
10851094
let box1Price = 0;
10861095
if ($box1Select.length) {
1087-
const $sel = $box1Select.find('option:selected');
1088-
const raw = String($sel.attr('data-surcharge-final-price') || $sel.attr('data-surcharge-additional-price') || '');
1089-
if (raw && raw.replace(/[^0-9]/g, '') !== '') {
1090-
box1Price = Number(raw.replace(/[^0-9]/g, ''));
1096+
const $sel = $box1Select.find("option:selected");
1097+
const raw = String($sel.attr("data-surcharge-final-price") || $sel.attr("data-surcharge-additional-price") || "");
1098+
if (raw && raw.replace(/[^0-9]/g, "") !== "") {
1099+
box1Price = Number(raw.replace(/[^0-9]/g, ""));
10911100
} else {
1092-
const $first = $box1Select.find('option[data-surcharge-final-price]:not([value=""])').filter(function () {
1093-
return Number(String($(this).attr('data-surcharge-final-price') || $(this).attr('data-surcharge-additional-price') || '0').replace(/[^0-9]/g, '')) > 0;
1094-
}).first();
1101+
const $first = $box1Select
1102+
.find('option[data-surcharge-final-price]:not([value=""])')
1103+
.filter(function () {
1104+
return (
1105+
Number(
1106+
String($(this).attr("data-surcharge-final-price") || $(this).attr("data-surcharge-additional-price") || "0").replace(/[^0-9]/g, ""),
1107+
) > 0
1108+
);
1109+
})
1110+
.first();
10951111
if ($first.length) {
1096-
box1Price = Number(String($first.attr('data-surcharge-final-price') || $first.attr('data-surcharge-additional-price') || '0').replace(/[^0-9]/g, ''));
1112+
box1Price = Number(
1113+
String($first.attr("data-surcharge-final-price") || $first.attr("data-surcharge-additional-price") || "0").replace(/[^0-9]/g, ""),
1114+
);
10971115
}
10981116
}
10991117
}
1100-
const $box1PriceEl = $(".parameter-wrap.parameter-" + box1).find('.price.price-standart');
1101-
$box1PriceEl.attr('data-price', box1Price);
1102-
if ($box1PriceEl.length) $box1PriceEl.text(box1Price > 0 ? NumToPrice(box1Price) : '0 Kč');
1118+
const $box1PriceEl = $(".parameter-wrap.parameter-" + box1).find(".price.price-standart");
1119+
$box1PriceEl.attr("data-price", box1Price);
1120+
if ($box1PriceEl.length) $box1PriceEl.text(box1Price > 0 ? NumToPrice(box1Price) : "0 Kč");
11031121
}
11041122
}
11051123
// Delay for price update

0 commit comments

Comments
 (0)