feat(02-product-actions-fixes): Phase 02 complete — customization, price label, structure fix
Plan 02-03: Customization save + success modal (5/5 AC)
- 26-field squaremeter POST payload (verbose PL dim, qty_alt/qty_alth)
- Chain POST /module/ps_shoppingcart/ajax -> Bootstrap #blockcart-modal
- Critical fix: moved {/block} so inline script actually renders
- __p02p02InFlight re-entrancy guard
Plan 02-04: Live cena per-sqm label obok "Dodaj do koszyka" (5/5 AC)
- .p02p04-total-price label, gorna .current-price static
- Separate __p02p04Bound + setInterval reconciliation
- Poll-retry prestashop.on registration
Plan 02-05: Struktura materialu w POST payload (4/4 AC)
- Enumerate [name^="group["] spoza formy, doklej do payload
- Fix: group_5 select w .product-bar-box nie trafial do koszyka
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -753,7 +753,6 @@
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
{/block}
|
||||
|
||||
{* =========================================================================
|
||||
Phase 02 Plan 02-02: inline add-to-cart handler (cache-buster).
|
||||
@@ -782,7 +781,11 @@
|
||||
e.stopImmediatePropagation();
|
||||
e.stopPropagation();
|
||||
|
||||
if (window.__p02p02InFlight) return;
|
||||
window.__p02p02InFlight = true;
|
||||
|
||||
if (!jQuery('#checkbox-piece').is(':checked')) {
|
||||
window.__p02p02InFlight = false;
|
||||
jQuery.fancybox({
|
||||
minWidth: 800, maxWidth: 1000, padding: 30, height: 100,
|
||||
content: 'Proszę wybrać rozmiar i wycinek tapety przed dodaniem jej do koszyka.'
|
||||
@@ -802,7 +805,65 @@
|
||||
$btn.prop('disabled', true).addClass('loading');
|
||||
|
||||
var qty = parseInt(jQuery('#quantity_wanted').val(), 10) || 1;
|
||||
var payload = $form.serialize() + '&qty=' + encodeURIComponent(qty) + '&add=1&action=update';
|
||||
|
||||
// Phase 02 Plan 02-03 Task 2: inject squaremeter fields (patrz custom.js komentarz).
|
||||
var pwRaw = parseInt(jQuery('#piece-width').val(), 10) || 100;
|
||||
var phRaw = parseInt(jQuery('#piece-height').val(), 10) || 100;
|
||||
var wM = (pwRaw / 100).toFixed(1);
|
||||
var hM = (phRaw / 100).toFixed(1);
|
||||
var areaM2 = (parseFloat(wM) * parseFloat(hM)).toFixed(4);
|
||||
var basePrice = parseFloat(jQuery('#product_base_price').val())
|
||||
|| parseFloat(jQuery('#product_fixed_price').val())
|
||||
|| parseFloat(jQuery('meta[property="product:price:amount"]').attr('content'))
|
||||
|| parseFloat((jQuery('.product-prices .current-price, .current-price').first().text() || '').replace(/[^\d.,]/g, '').replace(',', '.'))
|
||||
|| 0;
|
||||
var totalPriceCalc = Math.round(basePrice * parseFloat(areaM2));
|
||||
|
||||
jQuery('#discretion').prop('checked', true);
|
||||
if (jQuery('#product_total_price_calc').length) jQuery('#product_total_price_calc').val(totalPriceCalc);
|
||||
if (jQuery('#calculated_total').length) jQuery('#calculated_total').val(areaM2);
|
||||
if (jQuery('#converted_ea').length) jQuery('#converted_ea').val(areaM2);
|
||||
if (jQuery('#grand_calculated_total').length) jQuery('#grand_calculated_total').val('');
|
||||
if (jQuery('#extrafeevalue').length) jQuery('#extrafeevalue').val('0');
|
||||
if (jQuery('#wastevalue').length) jQuery('#wastevalue').val('0');
|
||||
if (jQuery('#quantity_wanted_alt').length) jQuery('#quantity_wanted_alt').val(wM);
|
||||
if (jQuery('#quantity_wanted_alth').length) jQuery('#quantity_wanted_alth').val(hM);
|
||||
|
||||
var isRefl = parseInt(jQuery('#product_is_reflection').val(), 10) || 0;
|
||||
var cropPosX = parseInt(jQuery('#product_crop_pos_x').val(), 10) || 0;
|
||||
var cropPosY = parseInt(jQuery('#product_crop_pos_y').val(), 10) || 0;
|
||||
var bgTop = jQuery('#piece_bg_top').val() || 0;
|
||||
var bgLeft = jQuery('#piece_bg_left').val() || 0;
|
||||
var dimStr = 'Szerokość ' + wM + ' m, Wysokość ' + hM + ' m, ' +
|
||||
parseFloat(areaM2).toFixed(2) + ' m2, Pozycja ' + cropPosX + ' ' + cropPosY +
|
||||
' <span> ,Pozycja tła ' + bgTop + ' ' + bgLeft + ' ,Odbicie ' + isRefl + ' </span>';
|
||||
if (jQuery('#dim').length) jQuery('#dim').val(dimStr);
|
||||
|
||||
var sqFields = 'discretion=on' +
|
||||
'&dim=' + encodeURIComponent(dimStr) +
|
||||
'&converted_ea=' + encodeURIComponent(areaM2) +
|
||||
'&calculated_total=' + encodeURIComponent(areaM2) +
|
||||
'&grand_calculated_total=' +
|
||||
'&extrafeevalue=0' +
|
||||
'&wastevalue=0' +
|
||||
'&product_total_price_calc=' + encodeURIComponent(totalPriceCalc) +
|
||||
'&qty_alt=' + encodeURIComponent(wM) +
|
||||
'&qty_alth=' + encodeURIComponent(hM);
|
||||
|
||||
// Plan 02-05: PS attribute groups POZA formą #add-to-cart-or-refresh (np. #group_5
|
||||
// "Tekstura materiału" w .product-bar-box). Enumeruj i dołącz do payload.
|
||||
var externalGroups = '';
|
||||
jQuery('[name^="group["]').each(function() {
|
||||
var $el = jQuery(this);
|
||||
if ($el.closest('#add-to-cart-or-refresh').length) return;
|
||||
var t = ($el.attr('type') || '').toLowerCase();
|
||||
if ((t === 'radio' || t === 'checkbox') && !$el.prop('checked')) return;
|
||||
var n = $el.attr('name');
|
||||
var v = $el.val();
|
||||
if (v === undefined || v === null || v === '') return;
|
||||
externalGroups += '&' + encodeURIComponent(n) + '=' + encodeURIComponent(v);
|
||||
});
|
||||
var payload = $form.serialize() + '&qty=' + encodeURIComponent(qty) + externalGroups + '&' + sqFields + '&add=1&action=update';
|
||||
var actionUrl = $form.attr('action') || window.location.href;
|
||||
|
||||
jQuery.ajax({
|
||||
@@ -831,14 +892,41 @@
|
||||
}
|
||||
jQuery(document).trigger('updatedCart', [resp]);
|
||||
|
||||
if (window.prestashop && prestashop.urls && prestashop.urls.pages && prestashop.urls.pages.cart) {
|
||||
jQuery.get(prestashop.urls.pages.cart, { action: 'refresh', ajax: 1 }, null, 'json')
|
||||
.done(function(cartResp) {
|
||||
if (cartResp && cartResp.preview) {
|
||||
jQuery('.blockcart').replaceWith(cartResp.preview);
|
||||
}
|
||||
});
|
||||
}
|
||||
// Phase 02 Plan 02-03 Task 3: fetch success modal + preview (patrz custom.js).
|
||||
jQuery.ajax({
|
||||
url: '/module/ps_shoppingcart/ajax',
|
||||
type: 'POST',
|
||||
data: {
|
||||
action: 'add-to-cart',
|
||||
id_product: resp.id_product || '',
|
||||
id_product_attribute: resp.id_product_attribute || '',
|
||||
id_customization: resp.id_customization || 0
|
||||
},
|
||||
dataType: 'json'
|
||||
}).done(function(mr) {
|
||||
if (mr && mr.preview) {
|
||||
jQuery('.blockcart').replaceWith(mr.preview);
|
||||
}
|
||||
if (mr && mr.modal) {
|
||||
jQuery('#blockcart-modal').remove();
|
||||
jQuery('.modal-backdrop').remove();
|
||||
jQuery('body').append(mr.modal);
|
||||
var $modal = jQuery('#blockcart-modal');
|
||||
if (typeof $modal.modal === 'function') {
|
||||
$modal.modal('show');
|
||||
} else {
|
||||
$modal.addClass('show in').css('display', 'block').attr('aria-hidden', 'false');
|
||||
jQuery('body').addClass('modal-open').append('<div class="modal-backdrop fade in show"></div>');
|
||||
$modal.on('click.blockcartClose', '[data-dismiss=modal], .close', function() {
|
||||
$modal.removeClass('show in').css('display', 'none').remove();
|
||||
jQuery('body').removeClass('modal-open');
|
||||
jQuery('.modal-backdrop').remove();
|
||||
});
|
||||
}
|
||||
}
|
||||
}).fail(function() {
|
||||
if (resp.cart) { jQuery('.cart-products-count').text(resp.cart.products_count || ''); }
|
||||
});
|
||||
|
||||
$btn.addClass('added-flash');
|
||||
setTimeout(function() { $btn.removeClass('added-flash'); }, 1200);
|
||||
@@ -851,10 +939,92 @@
|
||||
},
|
||||
complete: function() {
|
||||
$btn.prop('disabled', false).removeClass('loading');
|
||||
window.__p02p02InFlight = false;
|
||||
}
|
||||
});
|
||||
}, true);
|
||||
})();
|
||||
|
||||
/* =========================================================================
|
||||
Phase 02 Plan 02-04: live cena per-sqm (inline mirror).
|
||||
Separate IIFE + __p02p04Bound guard — niezalezny od __p02p02Bound, zeby
|
||||
stale-cache custom.js (bez Plan 02-04) nie blokowalo tej rejestracji.
|
||||
Kod identyczny z custom.js __p02p04 block (jQuery zamiast $).
|
||||
========================================================================= */
|
||||
(function() {
|
||||
if (window.__p02p04Bound) return;
|
||||
window.__p02p04Bound = true;
|
||||
|
||||
window.__p02p04EnsureLabel = function() {
|
||||
var $label = jQuery('.p02p04-total-price');
|
||||
if ($label.length) return $label;
|
||||
var $add = jQuery('.product-quantity .add').first();
|
||||
if (!$add.length) return jQuery();
|
||||
$label = jQuery('<span class="p02p04-total-price" aria-live="polite" style="display:inline-block;margin-right:16px;font-weight:700;font-size:1.25rem;vertical-align:middle;"></span>');
|
||||
$add.before($label);
|
||||
return $label;
|
||||
};
|
||||
|
||||
window.__p02p04RecalcPrice = function() {
|
||||
if (!document.querySelector('.product-variants-data--new')) return;
|
||||
|
||||
var pwRaw = parseInt((jQuery('#piece-width').val() || 0), 10) || 0;
|
||||
var phRaw = parseInt((jQuery('#piece-height').val() || 0), 10) || 0;
|
||||
|
||||
var $label = window.__p02p04EnsureLabel();
|
||||
|
||||
if (pwRaw <= 0 || phRaw <= 0) {
|
||||
if ($label.length) $label.text('');
|
||||
return;
|
||||
}
|
||||
|
||||
var areaM2 = (pwRaw / 100) * (phRaw / 100);
|
||||
|
||||
var basePrice = parseFloat(jQuery('meta[property="product:price:amount"]').attr('content'))
|
||||
|| parseFloat((jQuery('.product-prices .current-price, .current-price').first()
|
||||
.text() || '').replace(/[^\d.,]/g, '').replace(',', '.'))
|
||||
|| 0;
|
||||
if (basePrice <= 0) return;
|
||||
|
||||
var total = basePrice * areaM2;
|
||||
var formatted = total.toFixed(2).replace('.', ',') + ' zł';
|
||||
|
||||
if ($label.length) $label.text(formatted);
|
||||
};
|
||||
|
||||
jQuery(document).on('input change keyup', '#piece-width, #piece-height', function() {
|
||||
clearTimeout(window.__p02p04RecalcT);
|
||||
window.__p02p04RecalcT = setTimeout(window.__p02p04RecalcPrice, 100);
|
||||
});
|
||||
|
||||
// Initial render — interval re-apply przez 5s (squaremeter init overwrituje
|
||||
// .current-price po naszym pierwszym recalc). Patrz custom.js komentarz.
|
||||
window.__p02p04TryInitial = function() {
|
||||
var attempts = 0;
|
||||
var iv = setInterval(function() {
|
||||
attempts++;
|
||||
window.__p02p04RecalcPrice();
|
||||
if (attempts >= 10) clearInterval(iv);
|
||||
}, 500);
|
||||
};
|
||||
// Uruchom synchronicznie — jQuery ready w kontekscie inline Smarty block
|
||||
// nie firuje konsekwentnie. Recalc early-return gdy inputy brak.
|
||||
window.__p02p04TryInitial();
|
||||
|
||||
(function bindUpdatedProduct() {
|
||||
if (window.prestashop && typeof prestashop.on === 'function') {
|
||||
prestashop.on('updatedProduct', function() {
|
||||
setTimeout(window.__p02p04RecalcPrice, 50);
|
||||
});
|
||||
} else {
|
||||
setTimeout(bindUpdatedProduct, 200);
|
||||
}
|
||||
})();
|
||||
jQuery(document).on('updatedProduct', function() {
|
||||
setTimeout(window.__p02p04RecalcPrice, 50);
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
{/block}{* /block name=content — Plan 02-03 fix: moved to wrap inline script so it renders in {extends} template *}
|
||||
{/if}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user