{% sw_extends '@Storefront/storefront/page/product-detail/index.html.twig' %}
{% block page_product_detail_content %}
{{ parent() }}
{% if page.product.extensions.dkcProductOptions is defined and page.product.extensions.dkcProductOptions|length > 0 %}
<script>
document.addEventListener('DOMContentLoaded', function() {
var addToCartForm = document.querySelector('form[action*="checkout/line-item/add"]');
if (!addToCartForm) return;
// === Live-Preisanzeige ===
function parsePriceDE(text) {
if (!text) return 0;
var clean = text.replace(/[^\d,.\-]/g, '');
if (!clean) return 0;
if (clean.indexOf(',') !== -1) {
var parts = clean.split(',');
var intPart = parts[0].replace(/\./g, '');
var decPart = parts[1] || '0';
return parseFloat(intPart + '.' + decPart) || 0;
}
var lastDot = clean.lastIndexOf('.');
if (lastDot !== -1 && clean.length - lastDot === 3) {
var beforeDot = clean.substring(0, lastDot).replace(/\./g, '');
return parseFloat(beforeDot + '.' + clean.substring(lastDot + 1)) || 0;
}
return parseFloat(clean.replace(/\./g, '')) || 0;
}
function formatCurrency(value) {
return value.toLocaleString('de-DE', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
}
var priceElement = document.querySelector('.product-detail-price');
var taxContainer = document.querySelector('.product-detail-tax-container');
if (!taxContainer) taxContainer = document.querySelector('.product-detail-tax');
var baseDisplayPrice = 0;
if (priceElement) {
baseDisplayPrice = parsePriceDE(priceElement.textContent);
}
var taxRate = 19;
var displayIsNet = false;
var grossLine = null;
var baseGrossPrice = 0;
if (taxContainer) {
var taxText = taxContainer.textContent || '';
var rm = taxText.match(/(\d+)%\s*MwSt/i);
if (rm) taxRate = parseInt(rm[1]);
if (taxText.indexOf('exkl.') !== -1) displayIsNet = true;
var taxP = taxContainer.querySelector('.product-detail-tax') || taxContainer.querySelector('p');
if (taxP) {
var childNodes = taxP.childNodes;
for (var i = 0; i < childNodes.length; i++) {
if (childNodes[i].nodeType === 3) {
var nodeText = childNodes[i].textContent || '';
if (nodeText.indexOf('inkl.') !== -1) {
grossLine = childNodes[i];
baseGrossPrice = parsePriceDE(nodeText);
}
}
}
}
}
var baseNet, baseGross;
if (displayIsNet) {
baseNet = baseDisplayPrice;
baseGross = baseGrossPrice > 0 ? baseGrossPrice : baseNet * (1 + taxRate / 100);
} else {
baseGross = baseDisplayPrice;
baseNet = baseGross / (1 + taxRate / 100);
}
function getSelectedOptionsTotal() {
var total = 0;
document.querySelectorAll('.dkc-product-option-radio').forEach(function(r) {
if (r.checked && r.value !== '') total += parseFloat(r.dataset.price || 0);
});
document.querySelectorAll('.dkc-product-option-checkbox').forEach(function(c) {
if (c.checked) total += parseFloat(c.dataset.price || 0);
});
return total;
}
function updatePriceDisplay() {
var optionsTotal = getSelectedOptionsTotal();
var newNet = baseNet + optionsTotal;
var newGross = newNet * (1 + taxRate / 100);
var newDisplayPrice = displayIsNet ? newNet : newGross;
if (priceElement) {
priceElement.innerHTML = formatCurrency(newDisplayPrice) + ' €*';
}
if (grossLine) {
var oldText = grossLine.textContent;
grossLine.textContent = oldText.replace(
/[\d.,]+\s*[\u00a0\s]*\u20ac/,
formatCurrency(newGross) + '\u00a0\u20ac'
);
}
}
document.querySelectorAll('.dkc-product-option-radio, .dkc-product-option-checkbox').forEach(function(input) {
input.addEventListener('change', updatePriceDisplay);
});
// Bei Reload: sofort pruefen ob etwas angehakt ist
updatePriceDisplay();
// === Warenkorb Submit-Handler ===
addToCartForm.addEventListener('submit', function(event) {
var selectedOptions = [];
document.querySelectorAll('.dkc-product-option-radio').forEach(function(radio) {
if (radio.checked && radio.value !== '') {
selectedOptions.push({
id: radio.value,
name: radio.dataset.optionName,
price: parseFloat(radio.dataset.price || 0),
group: radio.dataset.group
});
}
});
document.querySelectorAll('.dkc-product-option-checkbox').forEach(function(cb) {
if (cb.checked) {
selectedOptions.push({
id: cb.value,
name: cb.dataset.optionName,
price: parseFloat(cb.dataset.price || 0),
group: cb.dataset.group
});
}
});
if (selectedOptions.length === 0) return;
var lineItemKey = null;
var lineItemRegex = /lineItems\[([^\]]+)\]/;
var inputs = addToCartForm.querySelectorAll('input[name^="lineItems"]');
for (var i = 0; i < inputs.length; i++) {
var match = inputs[i].name.match(lineItemRegex);
if (match && match[1]) {
lineItemKey = match[1];
break;
}
}
if (!lineItemKey) return;
addToCartForm.querySelectorAll('input[name*="dkc_product_options"]').forEach(function(el) { el.remove(); });
var optionsInput = document.createElement('input');
optionsInput.type = 'hidden';
optionsInput.name = 'lineItems[' + lineItemKey + '][payload][dkc_product_options]';
optionsInput.value = JSON.stringify(selectedOptions);
addToCartForm.appendChild(optionsInput);
var totalPrice = 0;
for (var j = 0; j < selectedOptions.length; j++) {
totalPrice += (selectedOptions[j].price || 0);
}
if (totalPrice > 0) {
var priceInput = document.createElement('input');
priceInput.type = 'hidden';
priceInput.name = 'lineItems[' + lineItemKey + '][payload][dkc_product_options_price]';
priceInput.value = totalPrice.toString();
addToCartForm.appendChild(priceInput);
}
var labels = [];
for (var k = 0; k < selectedOptions.length; k++) {
labels.push(selectedOptions[k].name);
}
if (labels.length > 0) {
var labelsInput = document.createElement('input');
labelsInput.type = 'hidden';
labelsInput.name = 'lineItems[' + lineItemKey + '][payload][dkc_product_options_labels]';
labelsInput.value = JSON.stringify(labels);
addToCartForm.appendChild(labelsInput);
}
});
});
</script>
{% endif %}
{% endblock %}