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); // Copy license key $(document).on('click', '.wpdd-copy-license', this.copyLicenseKey); }, 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 = $('
'; if (product.is_free) { content += 'Free'; } else { content += '$' + product.final_price + ''; } content += '
'; content += 'by ' + product.creator.name + '
'; content += 'Files: ' + product.files_count + '
'; content += 'Download Limit: ' + product.download_limit + '
'; content += 'Expires: ' + product.download_expiry + '
'; content += '' + message + '
' + '' + '