jQuery(document).ready(function($) { 'use strict'; /** * Main WPDD Frontend Object */ window.WPDD = { init: function() { this.bindEvents(); this.initProductCards(); this.initCheckout(); }, bindEvents: function() { // Add to cart buttons $(document).on('click', '.wpdd-add-to-cart', this.addToCart); // Free download buttons $(document).on('click', '.wpdd-free-download', this.processFreeDownload); // Product quickview $(document).on('click', '.wpdd-quickview', this.showQuickview); // Filter form submission $(document).on('submit', '.wpdd-filter-form', this.handleFilters); // Download status check $(document).on('click', '.wpdd-check-download', this.checkDownloadStatus); }, initProductCards: function() { // Add hover effects and animations $('.wpdd-product-card').each(function() { var $card = $(this); var $image = $card.find('.wpdd-product-image img'); $card.hover( function() { $(this).addClass('hovered'); }, function() { $(this).removeClass('hovered'); } ); }); }, initCheckout: function() { // Free product checkout handler $('#wpdd-checkout-form').on('submit', function(e) { var $form = $(this); var isFree = $form.data('product-free') == '1'; if (isFree) { e.preventDefault(); WPDD.processFreeCheckout($form); } }); // Price display toggle based on free checkbox $('#wpdd_is_free').on('change', function() { var $priceFields = $('.wpdd-price-field'); if ($(this).is(':checked')) { $priceFields.hide(); } else { $priceFields.show(); } }); }, addToCart: function(e) { e.preventDefault(); var $button = $(this); var productId = $button.data('product-id'); if (!productId) { WPDD.showNotice('Invalid product', 'error'); return; } $button.addClass('loading').prop('disabled', true); $.ajax({ url: wpdd_ajax.url, type: 'POST', data: { action: 'wpdd_add_to_cart', product_id: productId, nonce: wpdd_ajax.nonce }, success: function(response) { if (response.success) { WPDD.showNotice(response.data.message, 'success'); // Update cart count if element exists $('.wpdd-cart-count').text(response.data.cart_count); // Show checkout button if (response.data.checkout_url) { $button.after('Checkout'); } } else { WPDD.showNotice(response.data, 'error'); } }, error: function() { WPDD.showNotice('An error occurred. Please try again.', 'error'); }, complete: function() { $button.removeClass('loading').prop('disabled', false); } }); }, processFreeDownload: function(e) { e.preventDefault(); var $button = $(this); var productId = $button.data('product-id'); var $form = $button.closest('form'); if (!productId) { WPDD.showNotice('Invalid product', 'error'); return; } var customerEmail = $form.find('input[name="customer_email"]').val(); var customerName = $form.find('input[name="customer_name"]').val(); if (!customerEmail && !WPDD.isUserLoggedIn()) { WPDD.showNotice('Please provide your email address', 'error'); return; } $button.addClass('loading').prop('disabled', true); $.ajax({ url: wpdd_ajax.url, type: 'POST', data: { action: 'wpdd_process_free_download', product_id: productId, customer_email: customerEmail, customer_name: customerName, nonce: wpdd_ajax.nonce }, success: function(response) { if (response.success) { window.location.href = response.data.redirect_url; } else { WPDD.showNotice(response.data, 'error'); } }, error: function() { WPDD.showNotice('An error occurred. Please try again.', 'error'); }, complete: function() { $button.removeClass('loading').prop('disabled', false); } }); }, processFreeCheckout: function($form) { var formData = $form.serialize(); var $submitBtn = $form.find('button[type="submit"]'); var productId = $form.find('input[name="product_id"]').val(); $submitBtn.addClass('loading').prop('disabled', true); // Add visual feedback $submitBtn.text('Processing...'); $.ajax({ url: wpdd_ajax.url, type: 'POST', data: formData + '&action=wpdd_process_free_download&nonce=' + wpdd_ajax.nonce + '&product_id=' + productId, success: function(response) { if (response.success) { window.location.href = response.data.redirect_url; } else { WPDD.showNotice(response.data || 'Failed to process download', 'error'); $submitBtn.text('Get Free Download'); } }, error: function(xhr, status, error) { console.error('AJAX Error:', status, error); WPDD.showNotice('An error occurred. Please try again.', 'error'); $submitBtn.text('Get Free Download'); }, complete: function() { $submitBtn.removeClass('loading').prop('disabled', false); } }); }, showQuickview: function(e) { e.preventDefault(); var productId = $(this).data('product-id'); if (!productId) { return; } $.ajax({ url: wpdd_ajax.url, type: 'POST', data: { action: 'wpdd_get_product_details', product_id: productId, nonce: wpdd_ajax.nonce }, success: function(response) { if (response.success) { WPDD.displayQuickview(response.data); } else { WPDD.showNotice('Unable to load product details', 'error'); } }, error: function() { WPDD.showNotice('An error occurred. Please try again.', 'error'); } }); }, displayQuickview: function(product) { var modal = $('
'); var content = ''; content += '
'; content += '

' + product.title + '

'; content += ''; content += '
'; content += '
'; if (product.thumbnail) { content += '' + product.title + ''; } content += '
'; content += '

'; if (product.is_free) { content += 'Free'; } else { content += '$' + product.final_price + ''; } content += '

'; content += '

by ' + product.creator.name + '

'; content += '
' + product.description + '
'; content += '

Files: ' + product.files_count + '

'; content += '

Download Limit: ' + product.download_limit + '

'; content += '

Expires: ' + product.download_expiry + '

'; content += '
'; content += '
'; content += ''; modal.find('.wpdd-modal-content').html(content); $('body').append(modal); modal.addClass('active'); // Close modal events modal.on('click', '.wpdd-modal-close, .wpdd-modal', function(e) { if (e.target === this) { modal.removeClass('active'); setTimeout(function() { modal.remove(); }, 300); } }); }, handleFilters: function(e) { // Let the form submit naturally for now // Could be enhanced with AJAX filtering }, checkDownloadStatus: function(e) { e.preventDefault(); var productId = $(this).data('product-id'); var $button = $(this); if (!WPDD.isUserLoggedIn()) { WPDD.showNotice('Please login to check download status', 'error'); return; } $button.addClass('loading'); $.ajax({ url: wpdd_ajax.url, type: 'POST', data: { action: 'wpdd_check_download_status', product_id: productId, nonce: wpdd_ajax.nonce }, success: function(response) { if (response.success) { var data = response.data; if (data.can_download && data.download_url) { window.location.href = data.download_url; } else { WPDD.showNotice(data.message, data.can_download ? 'success' : 'error'); } } else { WPDD.showNotice(response.data, 'error'); } }, error: function() { WPDD.showNotice('An error occurred. Please try again.', 'error'); }, complete: function() { $button.removeClass('loading'); } }); }, showNotice: function(message, type) { type = type || 'info'; var notice = $('
' + '

' + message + '

' + '' + '
'); $('body').prepend(notice); notice.addClass('active'); // Auto-dismiss after 5 seconds setTimeout(function() { notice.removeClass('active'); setTimeout(function() { notice.remove(); }, 300); }, 5000); // Manual dismiss notice.find('.wpdd-notice-dismiss').on('click', function() { notice.removeClass('active'); setTimeout(function() { notice.remove(); }, 300); }); }, isUserLoggedIn: function() { return $('body').hasClass('logged-in'); }, formatPrice: function(price) { return '$' + parseFloat(price).toFixed(2); } }; // Initialize frontend functionality WPDD.init(); // Add modal and notice styles if not already present if (!$('#wpdd-dynamic-styles').length) { var styles = ` `; $('head').append(styles); } });