jQuery(function($) { // 1. 加载定制选项 function loadCustomOptions() { let productId = $('button.single_add_to_cart_button').val() || $('input[name="add-to-cart"]').val(); if (!productId) { console.error('无法获取产品ID'); return; } $.ajax({ url: wc_custom_params.ajax_url, type: 'POST', data: { action: 'get_custom_product_options', product_id: productId, security: wc_custom_params.nonce }, success: function(response) { if (response.success) { renderCustomOptions(response.data); removeStickyBar(); // 初始化时移除粘性栏 } }, error: function(xhr, status, error) { console.error('加载定制选项失败:', error); } }); } // 2. 渲染定制选项 function renderCustomOptions(data) { let titleHtml = `
Customize The Product
`; let finalHtml = `
${titleHtml}`; // 1. 尺码选项(单选样式) if (data.sizes && data.sizes.length > 0) { let sizeHtml = `
${data.sizes.map(size => `
${size.name}
`).join('')}
`; finalHtml += sizeHtml; } // 2. 颜色选项(单选样式) if (data.colors && data.colors.length > 0) { let colorHtml = `
${data.colors.map(color => `
${color.name}
`).join('')}
`; finalHtml += colorHtml; } // 3. 补丁选项 if (data.patches && data.patches.length > 0) { let patchHtml = `
${data.patches.map(patch => `
补丁
+$${patch.price}
`).join('')}
`; finalHtml += patchHtml; } // 4. 球员选项 if (data.patches && data.patches.length > 0) { let playerHtml = `
Blank (No Name/Number)
${data.players && data.players.length > 0 ? `
Choose A Player
` : ''}
Add Yours (+$5 each)
${data.players && data.players.length > 0 ? ` ` : ''}
`; finalHtml += playerHtml; } // 5. 价格汇总区域 if ((data.patches && data.patches.length > 0) || (data.players && data.players.length > 0)) { finalHtml += `
Calculating...
`; } finalHtml += `
`; // 插入HTML到页面 if ((data.sizes && data.sizes.length > 0) || (data.colors && data.colors.length > 0) || (data.patches && data.patches.length > 0) || (data.players && data.players.length > 0)) { $(".cart").before(finalHtml); // 绑定事件 - 尺码选择 $(".size-selectable").on("click", function() { $(this).addClass("selected").siblings().removeClass("selected"); $(this).closest('.size-option-group').find('.size-error').hide(); }); // 绑定事件 - 颜色选择 $(".color-selectable").on("click", function() { $(this).addClass("selected").siblings().removeClass("selected"); $(this).closest('.color-option-group').find('.color-error').hide(); }); // 绑定事件 - 补丁选择 $(".patch-selectable").on("click", function() { $(this).toggleClass("selected").siblings().removeClass("selected"); updatePrice(); }); // 绑定事件 - 球员选项切换 $(".player-option").on("click", function() { $(this).addClass("selected") .siblings(".player-option").removeClass("selected"); const type = $(this).data("type"); if (type === "player-select") { $("#player-select-container").show(); $("#player-custom-container").hide(); } else if (type === "player-custom") { $("#player-select-container").hide(); $("#player-custom-container").show(); } else { $("#player-select-container").hide(); $("#player-custom-container").hide(); } updatePrice(); }); // 绑定事件 - 球员列表选择 $('#player-list').on('change', updatePrice); // 绑定事件 - 自定义球员姓名输入 $("#custom-player-name").on("input", function() { $(this).val($(this).val().replace(/[^A-Za-z ]/g, '')); updatePrice(); }); // 绑定事件 - 自定义球员号码输入 $("#custom-player-number").on("input", function() { let number = $(this).val(); $(this).val(number.replace(/[^0-9]/g, '')); if (number !== "") { let num = parseInt(number); if (num < 0 || num > 99) { $("#number-error").show(); } else { $("#number-error").hide(); } } else { $("#number-error").hide(); } updatePrice(); }); // 初始价格计算 updatePrice(); } } // 3. 更新价格显示 function updatePrice() { let quantity = parseInt($('input.qty').val()) || 1; let basePrice = parseFloat($(".price .woocommerce-Price-amount").eq(1).text().replace(/[^\d.]/g, '')) || parseFloat($(".price .woocommerce-Price-amount").eq(0).text().replace(/[^\d.]/g, '')); let patchPrice = $(".patch-selectable.selected").data("price") || 0; let playerPrice = 0; if ($(".player-option[data-type='player-select']").hasClass("selected")) { playerPrice = $("#player-list option:selected").data("price") || 0; } else if ($(".player-option[data-type='player-custom']").hasClass("selected")) { const hasName = $("#custom-player-name").val().trim().length > 0; const numberValue = $("#custom-player-number").val().trim(); const hasValidNumber = numberValue !== "" && parseInt(numberValue) >= 0 && parseInt(numberValue) <= 99; playerPrice = (hasName ? 5 : 0) + (hasValidNumber ? 5 : 0); } let customizationFeePerItem = patchPrice + playerPrice; let totalPrice = (basePrice + customizationFeePerItem) * quantity; let $priceContainer = $(".custom-option-group").last(); if (customizationFeePerItem > 0) { $priceContainer.show(); $('#customization-fee').show().find('.price-display').text( `+$${(customizationFeePerItem * quantity).toFixed(2)}` ); $('#total-price').text('$' + totalPrice.toFixed(2)); } else { $priceContainer.hide(); } } // 4. 添加定制数据到表单 function addCustomDataToForm($form) { let selectedSize = $('.size-selectable.selected'); let selectedColor = $('.color-selectable.selected'); let selectedPatch = $('.patch-selectable.selected'); $form.find('input[name^="custom_"]').remove(); // 尺码数据 if ($('.size-option-group').length > 0) { if (!selectedSize.length) { $('.size-option-group .size-error').show(); return false; } $form.append(``); } // 颜色数据 if ($('.color-option-group').length > 0) { if (!selectedColor.length) { $('.color-option-group .color-error').show(); return false; } $form.append(``); } // 补丁数据 if (selectedPatch.length) { $form.append(``); $form.append(``); $form.append(``); } // 球员数据 if ($(".player-option[data-type='player-select']").hasClass("selected")) { let selectedPlayer = $('#player-list option:selected'); if (selectedPlayer.val()) { $form.append(``); $form.append(``); } } else if ($(".player-option[data-type='player-custom']").hasClass("selected")) { let playerName = $("#custom-player-name").val().trim(); let playerNumber = $("#custom-player-number").val().trim(); if (playerNumber !== "") { let num = parseInt(playerNumber); if (num < 0 || num > 99) { $("#number-error").show(); return false; } } if (!playerName && !playerNumber) { $("#custom-player-error").show(); return false; } $("#custom-player-error").hide(); $("#number-error").hide(); let customValue = `${playerName || ''} #${playerNumber ? `${playerNumber}` : ''}`; let customPrice = (playerName ? 5 : 0) + (playerNumber ? 5 : 0); $form.append(``); $form.append(``); $form.append(``); $form.append(``); } return true; } // 5. 添加到购物车 $(document).on('click', '.single_add_to_cart_button', function(e) { e.preventDefault(); let $form = $('form.cart'); if (!addCustomDataToForm($form)) { $('html, body').animate({ scrollTop: $('.custom-option-group .required').first().offset().top - 100 }, 500); return; } $form.submit(); }); // 6. Buy Now操作 $(document).on('click', '.pisol_single_buy_now', function(e) { e.preventDefault(); const $form = $('form.cart'); if (!addCustomDataToForm($form)) { $('html, body').animate({ scrollTop: $('.custom-option-group .required').first().offset().top - 100 }, 500); return; } $form.append(''); const formData = new FormData($form[0]); fetch($form.attr('action'), { method: 'POST', body: formData, headers: { 'Cache-Control': 'no-cache' } }).then(response => { if (response.redirected) { window.location.href = response.url; } }); }); // 7. 数量变化 $(document).on('change', 'input.qty', function() { updatePrice(); }); // 初始化 loadCustomOptions(); });

Showing 1–16 of 173 results