// Custom JS for Crosselling Cart Module // Function to initialize or re-initialize the Slick carousel function initCartCarousel() { // Use the specific class for our cart carousel const carouselSelector = '.slick-custom-cart-carousel'; const $carouselElement = $(carouselSelector); // Check if the carousel element exists in the DOM if ($carouselElement.length > 0) { console.log('Croselling Cart: Found carousel element:', $carouselElement); // Check if Slick is already initialized on this element if ($carouselElement.hasClass('slick-initialized')) { console.log('Croselling Cart: Carousel already initialized, unslicking first.'); try { $carouselElement.slick('unslick'); } catch (e) { console.error('Croselling Cart: Error unslicking:', e); } } // Initialize Slick Carousel console.log('Croselling Cart: Initializing Slick...'); $carouselElement.slick({ infinite: false, slidesToShow: 2, slidesToScroll: 2, dots: false, // Keep it simple for the cart arrows: true, // Show arrows if more than 2 items responsive: [ { breakpoint: 768, // Adjust breakpoint if needed settings: { slidesToShow: 2, slidesToScroll: 2 } } ] }); console.log('Croselling Cart: Slick initialization attempted.'); } else { console.log('Croselling Cart: Carousel element not found in DOM yet.'); } } function initCartCarouselProduct() { // Use the specific class for our cart carousel const carouselSelector = '.product-crosselling .slick-custom-cart-carousel'; const $carouselElement = $(carouselSelector); // Check if the carousel element exists in the DOM if ($carouselElement.length > 0) { console.log('Croselling Cart: Found carousel element:', $carouselElement); // Check if Slick is already initialized on this element if ($carouselElement.hasClass('slick-initialized')) { console.log('Croselling Cart: Carousel already initialized, unslicking first.'); try { $carouselElement.slick('unslick'); } catch (e) { console.error('Croselling Cart: Error unslicking:', e); } } // Initialize Slick Carousel console.log('Croselling Cart: Initializing Slick...'); $carouselElement.slick({ infinite: false, slidesToShow: 4, dots: false, // Keep it simple for the cart arrows: true, // Show arrows if more than 2 items responsive: [ { breakpoint: 768, // Adjust breakpoint if needed settings: { slidesToShow: 2 } } ] }); console.log('Croselling Cart: Slick initialization attempted.'); } else { console.log('Croselling Cart: Carousel element not found in DOM yet.'); } } // Function to refresh cross-selling products via AJAX function refreshCrossSellingProducts() { console.log('Checking if cross-selling products need to be refreshed...'); // Check if cross-selling products already exist in the cart const crossSellingSection = document.querySelector('.crossselling-products-modal'); const productsContainer = crossSellingSection ? crossSellingSection.querySelector('.slick-crossselling-products') : null; // Only refresh if the section doesn't exist or is empty if (!crossSellingSection || !productsContainer || !productsContainer.children.length) { console.log('Cross-selling products don\'t exist, refreshing...'); // Get the cart ID from the DOM or use another method to identify the current cart let cartId = null; // Try to get cart ID from URL or DOM element if (typeof prestashop !== 'undefined' && prestashop.cart) { cartId = prestashop.cart.id; } // If we couldn't get cart ID, try to get it from the DOM if (!cartId) { const cartIdElement = document.querySelector('[data-cart-id]'); if (cartIdElement) { cartId = cartIdElement.getAttribute('data-cart-id'); } } // Make AJAX request to refresh cross-selling products $.ajax({ url: prestashop.urls.base_url + 'index.php?fc=module&module=croselling_cart&controller=ajax', type: 'POST', data: { action: 'refreshCrossSelling', cartId: cartId, ajax: true }, success: function (response) { try { const data = JSON.parse(response); if (data.success && data.html) { // Replace the cross-selling section with the new HTML if (crossSellingSection) { crossSellingSection.outerHTML = data.html; } else { // If section doesn't exist, append it to the cart content const cartContent = document.querySelector('#blockcart-content'); if (cartContent) { cartContent.insertAdjacentHTML('beforeend', data.html); } } // Re-initialize the carousel after content update setTimeout(initCartCarousel, 200); } } catch (e) { console.error('Error parsing cross-selling response:', e); } }, error: function (xhr, status, error) { console.error('Error refreshing cross-selling products:', error); } }); } else { console.log('Cross-selling products already exist, skipping refresh'); } } function hasProductParam() { const urlParams = new URLSearchParams(window.location.search); return urlParams.has('productCross'); } function initializeCrossellingCart() { document.querySelector('.crossselling-products-modal').style.display = 'block'; document.querySelector("#cart-toogle")?.addEventListener("click", () => { setTimeout(initCartCarousel, 500); // 500ms delay, adjust if needed setTimeout(() => { renderProductCombinations(2); }, 500); }); document.querySelector("#mobile-cart-toogle")?.addEventListener("click", () => { setTimeout(initCartCarousel, 500); // 500ms delay, adjust if needed setTimeout(() => { renderProductCombinations(2); }, 500); }); } document.addEventListener('DOMContentLoaded', () => { console.log('Crosselling Cart JS Loaded'); initializeCrossellingCart(); if (hasProductParam()) { document.querySelector('.product-crosselling').style.display = 'block'; initCartCarouselProduct(); attemptRenderProductCombinations(); // No change in behavior, but logic is now ready for future conditional behavior } // Listen for PrestaShop's cart update event if (typeof prestashop !== 'undefined') { prestashop.on('updatedCart', function (event) { // First refresh the cross-selling products refreshCrossSellingProducts(); // Then initialize the carousel (as a fallback) setTimeout(initCartCarousel, 500); // 500ms delay, adjust if needed setTimeout(() => { renderProductCombinations(2); }, 500); }); // Also listen for add to cart event prestashop.on('updateCart', function (event) { // Check if the cart was empty before this item was added (i.e., now has 1 item) if (prestashop.cart && prestashop.cart.products && prestashop.cart.products.length === 1) { refreshCrossSellingProducts(); setTimeout(initCartCarousel, 500); // 500ms delay, adjust if needed setTimeout(() => { renderProductCombinations(2); }, 500); } }); } });