1/* 2 * Koszyk: cena jednostkowa w wierszu i podtrzymanie jej przy przeliczeniach. 3 * 4 * Przeniesione ze snippetu WPCode 158142 (czÄÅÄ <script>), dotÄ d wypisywanego 5 * w stopce każdej podstrony. 6 * 7 * Napis âCena za sztukÄ" przychodzi z PHP (`window.stalesiaKoszyk`), wiÄc idzie 8 * za jÄzykiem strony przez zwykÅe tÅumaczenia motywu. 9 */ 10 11document.addEventListener('DOMContentLoaded', function() { 12 13 // 1. Sprawdzamy jÄzyk strony (WPML) na samym poczÄ tku 14 const teksty = window.stalesiaKoszyk || {}; 15 16 // 2. Definiujemy przetÅumaczony prefix 17 const unitPriceLabel = teksty.cenaZaSztuke || 'Cena za sztukÄ: '; 18 19 function initLivePriceSync() { 20 const rows = document.querySelectorAll('.wc-block-cart-items__row'); 21 22 rows.forEach(row => { 23 if (row.dataset.syncActive === 'true') return; 24 25 const hiddenTotalTd = row.querySelector('.wc-block-cart-item__total'); 26 const visiblePriceDiv = row.querySelector('.wc-block-cart-item__prices'); 27 28 if (hiddenTotalTd && visiblePriceDiv) { 29 30 if (!row.dataset.unitPriceHtml) { 31 row.dataset.unitPriceHtml = visiblePriceDiv.innerHTML; 32 } 33 34 const updatePrices = () => { 35 const currentTotalHtml = hiddenTotalTd.innerHTML; 36 const savedUnitPriceHtml = row.dataset.unitPriceHtml; 37
38 // 3. Używamy zmiennej unitPriceLabel wstrzykujÄ c HTML 39 visiblePriceDiv.innerHTML = ` 40 <div class="custom-total-price">${currentTotalHtml}</div> 41 <div class="custom-unit-price">(${unitPriceLabel}${savedUnitPriceHtml})</div> 42 `; 43 }; 44 45 updatePrices(); 46 47 const observer = new MutationObserver((mutations) => { 48 updatePrices(); 49 }); 50 51 observer.observe(hiddenTotalTd, { 52 subtree: true, 53 childList: true, 54 characterData: true 55 }); 56 57 row.dataset.syncActive = 'true'; 58 } 59 }); 60 } 61 62 const cartObserver = new MutationObserver((mutations) => { 63 const cartExists = document.querySelector('.wc-block-cart-items__row'); 64 if (cartExists) { 65 setTimeout(initLivePriceSync, 50); 66 } 67 }); 68 69 cartObserver.observe(document.body, { 70 childList: true, 71 subtree: true 72 }); 73 74 setTimeout(initLivePriceSync, 100); 75 }); 76 77/* 78 * OdÅwieżenie szuflady po dodaniu do koszyka (dawny snippet WPCode 165826). 79 * WooCommerce Blocks nasÅuchuje wÅasnych zdarzeÅ, a klasyczne âDo koszyka" 80 * wysyÅa tylko `added_to_cart` â bez tego mostka licznik i szuflada nie 81 * odÅwieżaÅy siÄ po dodaniu produktu z listy. 82 */ 83( function () { 84 if ( typeof jQuery === 'undefined' ) { 85 return; 86 } 87 jQuery( document ).on( 'added_to_cart', function () { 88 document.dispatchEvent( new CustomEvent( 'wc-blocks_added_to_cart' ) ); 89 document.dispatchEvent( new CustomEvent( 'cart_updated' ) ); 90 } ); 91}() );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.