PageSourceSearch

https://stalesia.com/wp-content/themes/stalesia/assets/js/koszyk.js?ver=1789557376

js stalesia.com collected 2026-10-06 23:56:35 UTC 3,382 bytes, 91 lines download raw bytes

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.