jQuery(function($) { 'use strict'; function cleanupCartForm() { var $area = $('.pbu-cart-area'); if (!$area.length) return; $area.find('label, h1, h2, h3, h4, h5, h6').each(function() { var $el = $(this); if ($el.hasClass('single_variation_wrap') || $el.hasClass('woocommerce-variation-add-to-cart') || $el.hasClass('variations') || $el.closest('.variations').length || $el.closest('button').length || $el.find('select, button, input').length) return; var txt = $el.clone().children().remove().end().text().trim().toLowerCase(); if (txt === 'add to cart' || txt === 'quantity' || txt === 'quantity:') $el.remove(); }); $area.find('.quantity').remove(); } cleanupCartForm(); $(document.body).on('woocommerce_variation_has_changed found_variation updated_wc_div', cleanupCartForm); var $track = $('#pbuCarouselTrack'); var $slides = $track.find('.pbu-slide'); var totalSlides = $slides.length; var currentSlide = 0; function goToSlide(index) { if (index < 0) index = totalSlides - 1; if (index >= totalSlides) index = 0; currentSlide = index; $track.css('transform', 'translateX(-' + (index * 100) + '%)'); $('.pbu-thumb').removeClass('active'); $('.pbu-thumb[data-index="' + index + '"]').addClass('active'); ensureThumbVisible(index); } $(document).on('click', '#pbuPrevImg', function(e) { e.preventDefault(); e.stopPropagation(); goToSlide(currentSlide - 1); }); $(document).on('click', '#pbuNextImg', function(e) { e.preventDefault(); e.stopPropagation(); goToSlide(currentSlide + 1); }); $(document).on('click', '.pbu-thumb', function(e) { e.preventDefault(); goToSlide(parseInt($(this).data('index'))); }); var touchStartX = 0, touchEndX = 0, isDragging = false, moveX = 0; var carousel = document.getElementById('pbuCarousel'); if (carousel) { carousel.addEventListener('touchstart', function(e) { touchStartX = e.changedTouches[0].screenX; isDragging = true; moveX = 0; }, { passive: true }); carousel.addEventListener('touchmove', function(e) { if (!isDragging) return; moveX = e.changedTouches[0].screenX - touchStartX; }, { passive: true }); carousel.addEventListener('touchend', function(e) { if (!isDragging) return; touchEndX = e.changedTouches[0].screenX; var diff = touchStartX - touchEndX; if (Math.abs(diff) > 50) { if (diff > 0) goToSlide(currentSlide + 1); else goToSlide(currentSlide - 1); } isDragging = false; setTimeout(function() { moveX = 0; }, 100); }, { passive: true }); carousel.addEventListener('mousedown', function() { moveX = 0; }); } $(document).on('click', '#pbuExpandIcon', function(e) { e.preventDefault(); e.stopPropagation(); openLightbox(currentSlide); }); $('#pbuCarousel').on('click', function(e) { if ($(e.target).closest('.pbu-nav-arrow, .pbu-expand-icon').length) return; if (Math.abs(moveX) > 5) return; openLightbox(currentSlide); }); var $thumbsTrack = $('#pbuThumbsTrack'); var $thumbs = $thumbsTrack.find('.pbu-thumb'); var thumbsOffset = 0; var THUMBS_PER_VIEW = 4; function getThumbWidth() { var $viewport = $thumbsTrack.parent(); var gap = 10; return ($viewport.width() - (gap * (THUMBS_PER_VIEW - 1))) / THUMBS_PER_VIEW + gap; } function moveThumbs(direction) { var maxOffset = Math.max(0, $thumbs.length - THUMBS_PER_VIEW); thumbsOffset += direction; if (thumbsOffset < 0) thumbsOffset = 0; if (thumbsOffset > maxOffset) thumbsOffset = maxOffset; var w = getThumbWidth(); $thumbsTrack.css('transform', 'translateX(-' + (thumbsOffset * w) + 'px)'); updateThumbNavState(); } function updateThumbNavState() { var maxOffset = Math.max(0, $thumbs.length - THUMBS_PER_VIEW); $('#pbuThumbsPrev').prop('disabled', thumbsOffset <= 0); $('#pbuThumbsNext').prop('disabled', thumbsOffset >= maxOffset); } function ensureThumbVisible(index) { var w = getThumbWidth(); if (index < thumbsOffset) { thumbsOffset = index; $thumbsTrack.css('transform', 'translateX(-' + (thumbsOffset * w) + 'px)'); updateThumbNavState(); } else if (index >= thumbsOffset + THUMBS_PER_VIEW) { thumbsOffset = index - THUMBS_PER_VIEW + 1; $thumbsTrack.css('transform', 'translateX(-' + (thumbsOffset * w) + 'px)'); updateThumbNavState(); } } $(document).on('click', '#pbuThumbsPrev', function(e) { e.preventDefault(); e.stopPropagation(); moveThumbs(-1); }); $(document).on('click', '#pbuThumbsNext', function(e) { e.preventDefault(); e.stopPropagation(); moveThumbs(1); }); updateThumbNavState(); var thumbTouchStart = 0, thumbTouchEnd = 0; var thumbsTrackEl = document.getElementById('pbuThumbsTrack'); if (thumbsTrackEl) { thumbsTrackEl.addEventListener('touchstart', function(e) { thumbTouchStart = e.changedTouches[0].screenX; }, { passive: true }); thumbsTrackEl.addEventListener('touchend', function(e) { thumbTouchEnd = e.changedTouches[0].screenX; var diff = thumbTouchStart - thumbTouchEnd; if (Math.abs(diff) > 50) { if (diff > 0) moveThumbs(1); else moveThumbs(-1); } }, { passive: true }); } var resizeTimer; $(window).on('resize', function() { clearTimeout(resizeTimer); resizeTimer = setTimeout(function() { thumbsOffset = 0; $thumbsTrack.css('transform', 'translateX(0)'); updateThumbNavState(); }, 150); }); var lightboxIndex = 0; function openLightbox(index) { lightboxIndex = index; updateLightboxImage(); $('#pbuLightbox').addClass('active'); $('body').css('overflow', 'hidden'); } function closeLightbox() { $('#pbuLightbox').removeClass('active'); $('body').css('overflow', ''); } function updateLightboxImage() { var $slide = $('.pbu-slide[data-index="' + lightboxIndex + '"]'); var fullSrc = $slide.data('full') || $slide.find('img').attr('src'); $('#pbuLightboxImg').attr('src', fullSrc); $('#pbuLightboxCounter').text((lightboxIndex + 1) + ' / ' + totalSlides); } function lightboxNav(direction) { lightboxIndex += direction; if (lightboxIndex < 0) lightboxIndex = totalSlides - 1; if (lightboxIndex >= totalSlides) lightboxIndex = 0; updateLightboxImage(); } $(document).on('click', '#pbuLightboxClose', closeLightbox); $(document).on('click', '#pbuLightboxPrev', function(e) { e.stopPropagation(); lightboxNav(-1); }); $(document).on('click', '#pbuLightboxNext', function(e) { e.stopPropagation(); lightboxNav(1); }); $('#pbuLightbox').on('click', function(e) { if (e.target.id === 'pbuLightbox' || $(e.target).hasClass('pbu-lightbox-container') || $(e.target).hasClass('pbu-lightbox-image-wrap')) closeLightbox(); }); var lbTouchStart = 0, lbTouchEnd = 0; var lightboxEl = document.getElementById('pbuLightbox'); if (lightboxEl) { lightboxEl.addEventListener('touchstart', function(e) { lbTouchStart = e.changedTouches[0].screenX; }, { passive: true }); lightboxEl.addEventListener('touchend', function(e) { lbTouchEnd = e.changedTouches[0].screenX; var diff = lbTouchStart - lbTouchEnd; if (Math.abs(diff) > 50) { if (diff > 0) lightboxNav(1); else lightboxNav(-1); } }, { passive: true }); } $(document).on('keydown', function(e) { if ($('#pbuLightbox').hasClass('active')) { if (e.key === 'Escape') closeLightbox(); if (e.key === 'ArrowLeft') lightboxNav(-1); if (e.key === 'ArrowRight') lightboxNav(1); return; } if ($('.pbu-modal.active').length) { if (e.key === 'Escape') { $('.pbu-modal').removeClass('active'); $('body').css('overflow', ''); } return; } if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' || e.target.tagName === 'SELECT') return; if (e.key === 'ArrowLeft') goToSlide(currentSlide - 1); if (e.key === 'ArrowRight') goToSlide(currentSlide + 1); }); // ═══════════════════════════════════════════════════════════════ // ACCORDION - Close all by default, only open on click // ═══════════════════════════════════════════════════════════════ $('.pbu-acc-item').removeClass('open'); var reviewsLoaded = false; $('.pbu-acc-header').on('click', function() { var $item = $(this).closest('.pbu-acc-item'); var $body = $item.find('.pbu-acc-body'); $item.toggleClass('open'); if ($body.data('lazy-reviews') && !reviewsLoaded && $item.hasClass('open')) { reviewsLoaded = true; if (typeof window.pbuReviewsData === 'undefined') return; $body.html('
Loading reviews...
'); $.ajax({ url: window.pbuReviewsData.ajaxUrl, type: 'POST', data: { action: 'pbu_get_reviews', nonce: window.pbuReviewsData.nonce, product_id: window.pbuReviewsData.productId }, success: function(response) { if (response.success) $body.html(response.data); else $body.html('Could not load reviews.
'); }, error: function() { $body.html('Could not load reviews.
'); } }); } }); $('#pbuSizeGuideBtn').on('click', function() { $('#pbuSizeModal').addClass('active'); $('body').css('overflow', 'hidden'); }); $('#pbuModalCloseBtn').on('click', function() { $('#pbuSizeModal').removeClass('active'); $('body').css('overflow', ''); }); $('#pbuSizeModal').on('click', function(e) { if (e.target.id === 'pbuSizeModal') { $(this).removeClass('active'); $('body').css('overflow', ''); } }); $('#pbuClearBtn').on('click', function() { var $form = $('.pbu-cart-area form.cart'); if (!$form.length) return; try { $form.find('.variations select').val(''); $form.find('input[name="variation_id"]').val('0'); } catch(e) {} }); $('#pbuQtyMinus').on('click', function() { var val = parseInt($('#pbuQtyInput').val()) || 1; if (val > 1) $('#pbuQtyInput').val(val - 1); }); $('#pbuQtyPlus').on('click', function() { var val = parseInt($('#pbuQtyInput').val()) || 1; if (val < 99) $('#pbuQtyInput').val(val + 1); }); $('#pbuQtyInput').on('change', function() { var val = parseInt($(this).val()) || 1; if (val < 1) val = 1; if (val > 99) val = 99; $(this).val(val); }); // ═══════════════════════════════════════════════════════════════ // ★★★ ADD TO CART - NUCLEAR FIX: Direct URL redirect ★★★ // ═══════════════════════════════════════════════════════════════ $('#pbuCustomAddToCart').on('click', function(e) { e.preventDefault(); e.stopPropagation(); var $btn = $(this); var $form = $('.pbu-cart-area form.cart').first(); var qty = parseInt($('#pbuQtyInput').val()) || 1; if (!$form.length) { alert('Cart form not found. Please refresh.'); return; } var isVariable = $form.hasClass('variations_form') || $form.find('input[name="variation_id"]').length > 0; // Collect selected attributes var selectedAttributes = {}; var allSelected = true; $form.find('.variations select').each(function() { var name = $(this).attr('name'); var val = $(this).val(); if (!val) { allSelected = false; return false; } selectedAttributes[name] = val; }); if (isVariable && !allSelected) { alert('Please select all product options first.'); return; } $btn.text('Adding...').prop('disabled', true); // Get product ID var productId = parseInt($form.find('input[name="product_id"]').val(), 10) || parseInt($form.find('button[name="add-to-cart"]').val(), 10) || parseInt($form.find('.single_add_to_cart_button').val(), 10) || parseInt($form.find('.single_add_to_cart_button').attr('data-product_id'), 10) || 0; // Get variation ID var variationId = parseInt($form.find('input[name="variation_id"]').val(), 10) || 0; // If variable but no variation_id, find it from product_variations data if (isVariable && !variationId) { var productVariations = $form.data('product_variations'); if (productVariations && productVariations.length) { for (var i = 0; i < productVariations.length; i++) { var variation = productVariations[i]; var match = true; for (var attrName in selectedAttributes) { var varAttr = variation.attributes[attrName]; if (varAttr && varAttr !== '' && varAttr !== selectedAttributes[attrName]) { match = false; break; } } if (match) { variationId = variation.variation_id; break; } } } } if (!productId) { alert('Product ID not found. Please refresh.'); $btn.text('Add To Cart').prop('disabled', false); return; } // ═══════════════════════════════════════════════ // BUILD URL - This is the KEY fix // ═══════════════════════════════════════════════ var currentUrl = window.location.href.split('?')[0]; var params = []; if (isVariable && variationId) { // Variable product: add-to-cart is the variation_id params.push('add-to-cart=' + variationId); params.push('variation_id=' + variationId); params.push('product_id=' + productId); params.push('quantity=' + qty); $.each(selectedAttributes, function(name, value) { params.push(encodeURIComponent(name) + '=' + encodeURIComponent(value)); }); } else { // Simple product params.push('add-to-cart=' + productId); params.push('quantity=' + qty); } var finalUrl = currentUrl + '?' + params.join('&'); // Redirect - WooCommerce will handle everything server-side window.location.href = finalUrl; }); $(document.body).on('added_to_cart', function() { $('.pbu-toast-msg').remove(); var $t = $('