1/** 2 * OS MINI CART CUSTOM LOGIC 3 * Gestione unificata per Mini Cart Desktop e Mobile in Bricks Builder. 4 * Include apertura automatica, apertura manuale, sincronizzazione barre e note ordine. 5 */ 6 7(function($) { 8 9 /** 10 * FUNZIONE UNIVERSALE PER APERTURA DRAWER 11 * Aggiunge la classe di Bricks per mostrare il carrello con un piccolo delay 12 * per assicurarsi che il DOM sia pronto dopo le chiamate AJAX. 13 */ 14 const forceOpenDrawer = () => { 15 const $miniCarts = $('.brxe-woocommerce-mini-cart'); 16 if ($miniCarts.length) { 17 setTimeout(() => { 18 $miniCarts.addClass('show-cart-details'); 19 }, 150); 20 } 21 }; 22 23 /** 24 * 1. GESTIONE APERTURA AUTOMATICA (Desktop & Mobile) 25 * Si attiva non appena WooCommerce conferma l'aggiunta di un prodotto via AJAX. 26 */ 27 $(document.body).on('added_to_cart', function() { 28 forceOpenDrawer(); 29 }); 30 31 /** 32 * 2. SETUP UI UNIVERSALE E SINCRONIZZAZIONE 33 * Gestisce la testata custom, la barra di spedizione e le note. 34 * Sincronizza i contenuti tra le istanze Desktop e Mobile per evitare "buchi" di contenuto. 35 */ 36 const setupUniversalCartUI = () => { 37 const $miniCarts = $('.widget_shopping_cart_content'); 38 const notesTemp = document.getElementById('cart-notes-template'); 39 40 // Sincronizzazione Barra Spedizione: copia il contenuto se una delle due istanze è vuota 41 const $allBars = $('#shipping-bar-wrapper'); 42 if ($allBars.length > 1) { 43 const masterContent = $allBars.first().html(); 44 if (masterContent && masterContent.trim() !== "") { 45 $allBars.each(function() { 46 if ($(this).html().trim() === "") { 47 $(this).html(masterContent); 48 } 49 }); 50 } 51 } 52 53 $miniCarts.each(function() { 54 const $currentCart = $(this); 55 const $subtotal = $currentCart.find('.woocommerce-mini-cart__total'); 56 57 // Iniezione Note dell'ordine se non presenti in questa specifica istanza 58 if (notesTemp && $subtotal.length && !$currentCart.find('.os-mini-cart-notes-wrapper').length) { 59 $subtotal.before(notesTemp.innerHTML); 60 initNoteEvents($currentCart); 61 } 62 63 // Nasconde header predefiniti di Bricks se presenti per evitare duplicati 64 $currentCart.find('.os_mini-cart-header').hide(); 65 }); 66 67 // Ripristina il valore salvato nel localStorage in tutte le textarea 68 const savedNote = localStorage.getItem('os_cart_note_temp'); 69 if (savedNote) { 70 $('textarea[id="os_cart_note"]').val(savedNote); 71 } 72 }; 73 74 /** 75 * 3. LOGICA GESTIONE NOTE ORDINE 76 * Gestisce l'apertura/chiusura della tendina note e la sincronizzazione real-time dei dati. 77 */ 78 const initNoteEvents = ($cart) => { 79 const $wrapper = $cart.find('.os-mini-cart-notes-wrapper'); 80 const $textarea = $wrapper.find('textarea'); 81 82 $wrapper.find('.os-notes-trigger').off('click').on('click', function(e) { 83 e.preventDefault(); 84 e.stopPropagation(); 85 const $content = $wrapper.find('.os-notes-content'); 86 const isHidden = $content.is(':hidden'); 87 $content.stop(true, true).slideToggle(250); 88 $wrapper.find('.os-notes-arrow').css('transform', isHidden ? 'rotate(180deg)' : 'rotate(0deg)'); 89 if (isHidden) setTimeout(() => $textarea.focus(), 300); 90 }); 91 92 $wrapper.find('.os-notes-close').off('click').on('click', function() { 93 $cart.find('.os-notes-content').slideUp(250); 94 $wrapper.find('.os-notes-arrow').css('transform', 'rotate(0deg)'); 95 }); 96 97 $textarea.on('input', function() { 98 const val = $(this).val(); 99 localStorage.setItem('os_cart_note_temp', val); 100 $('textarea[id="os_cart_note"]').val(val); // Sincronizza tutti i mini-cart 101 if ($('#order_comments').length) $('#order_comments').val(val);
101 // Sincronizza il checkout 102 }); 103 }; 104 105 /** 106 * 4. GESTIONE EVENTI CLICK (Apertura Manuale e Chiusura) 107 * Gestisce il click sull'icona carrello differenziando Desktop e Mobile. 108 */ 109 const initCartEvents = () => { 110 // Apertura manuale: clic sull'icona o sul titolo del carrello 111 $(document.body).off('click', '.bricks-woo-toggle, .cart-icon, .cart-title-enclosure') 112 .on('click', '.bricks-woo-toggle, .cart-icon, .cart-title-enclosure', function(e) { 113 const $parentCart = $(this).closest('.brxe-woocommerce-mini-cart'); 114 115 // Se siamo su Mobile (dentro il menu mobile di Bricks), forziamo l'azione 116 if ($(this).closest('.os_menu_mobile').length > 0) { 117 e.preventDefault(); 118 e.stopPropagation(); 119 $parentCart.addClass('show-cart-details'); 120 } 121 // Su Desktop lasciamo agire Bricks, ma aggiungiamo un safety trigger 122 else { 123 setTimeout(() => { 124 $parentCart.addClass('show-cart-details'); 125 }, 30); 126 } 127 }); 128 129 // Chiusura tramite la nostra "X" personalizzata 130 $(document.body).on('click', '.os-close-minicart', function(e) { 131 e.preventDefault(); 132 e.stopPropagation(); 133 $('.brxe-woocommerce-mini-cart').removeClass('show-cart-details'); 134 }); 135 136 // Chiusura al click all'esterno del carrello 137 $(document).on('click', function(e) { 138 const $miniCart = $('.brxe-woocommerce-mini-cart'); 139 if ($miniCart.hasClass('show-cart-details') && 140 !$miniCart.is(e.target) && 141 $miniCart.has(e.target).length === 0 && 142 !$(e.target).closest('.add_to_cart_button, .bricks-woo-toggle, .cart-icon').length) { 143 $miniCart.removeClass('show-cart-details'); 144 } 145 }); 146 147 // Chiusura con tasto ESC 148 $(document).on('keydown', function(e) { 149 if (e.key === 'Escape') $('.brxe-woocommerce-mini-cart').removeClass('show-cart-details'); 150 }); 151 }; 152 153 /** 154 * 5. INIZIALIZZAZIONE E MONITORAGGIO 155 */ 156 $(document).ready(function() { 157 initCartEvents(); 158 setupUniversalCartUI(); 159 160 // Sincronizzazione campo checkout se l'utente atterra direttamente lì 161 const saved = localStorage.getItem('os_cart_note_temp'); 162 if (saved && $('#order_comments').length) $('#order_comments').val(saved); 163 }); 164 165 // Ricarica la logica quando WooCommerce aggiorna i frammenti (AJAX) 166 $(document.body).on('wc_fragments_refreshed wc_fragments_loaded added_to_cart updated_wc_div', function() { 167 setTimeout(setupUniversalCartUI, 350); 168 }); 169 170 // Observer per monitorare l'apertura del menu mobile di Bricks 171 const observer = new MutationObserver(setupUniversalCartUI); 172 document.querySelectorAll('.brxe-woocommerce-mini-cart').forEach(cart => { 173 observer.observe(cart, { attributes: true, childList: true, subtree: true }); 174 }); 175 176})(jQuery);
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.