<script> jQuery(function($) { // === Helper Functions === function getAjaxUrl() { return (window.wc_cart_params && window.wc_cart_params.ajax_url) || '<?php echo esc_url(admin_url('admin-ajax.php')); ?>'; } function getCartNonce() { const n = $('input[name="woocommerce-cart-nonce"]').val(); return n || '<?php echo wp_create_nonce('woocommerce-cart'); ?>'; } function fmtRM(n) { return 'RM' + (isNaN(n) ? 0 : n).toFixed(2); } // === 全选同步功能 === function syncSelectAllStatus() { // 获取当前所有商品是否都被选中 const allChecked = $('.item-checkbox').length === $('.item-checkbox:checked').length; // 更新表头和页脚的全选状态 $('#select-all-items-header').prop('checked', allChecked); $('#select-all-items-footer').prop('checked', allChecked); } // === 更新选中商品摘要 === function updateSelectedSummary() { let count = 0, total = 0; $('.item-checkbox:checked').each(function() { const quantity = parseFloat($(this).data('quantity')) || 1; const price = parseFloat($(this).data('price')) || 0; count += quantity; total += price; }); $('#selected-count').text(count); $('#selected-total').text(fmtRM(total)); // 同步全选状态 syncSelectAllStatus(); } // === 保存数量修改 === function saveQuantityAjax($input, showNotice = true) { if (!$input || !$input.length) return; const $row = $input.closest('tr'); const cart_item_key = $row.find('.item-checkbox').val(); const newQty = parseFloat($input.val()) || 1; $input.prop('disabled', true); $row.find('.plus, .minus').prop('disabled', true); const payload = { action: 'update_cart_item_qty', security: getCartNonce(), cart_item_key: cart_item_key, qty: newQty }; $.post(getAjaxUrl(), payload, function(response) { if (response && response.success) { const d = response.data || {}; const $checkbox = $row.find('.item-checkbox'); // 更新小计列的HTML if (d.subtotal_html) { $row.find('.product-subtotal').html(d.subtotal_html); } // 更新复选框的数据 if (d.discounted_price) { $checkbox.data('price', d.discounted_price); $checkbox.attr('data-price', d.discounted_price); } else if (d.subtotal_html) { const priceValue = parseFloat( d.subtotal_html.replace(/[^\d.]/g, '') .replace(/\.(?=.*\.)/g, '') .replace(/,/g, '') ); if (!isNaN(priceValue)) { $checkbox.data('price', priceValue); $checkbox.attr('data-price', priceValue); } } if (d.unit_price) { $checkbox.data('unit-price', d.unit_price); $checkbox.attr('data-unit-price', d.unit_price); } // 更新数量属性 if (d.adjusted_qty) { $input.val(d.adjusted_qty); $checkbox.data('quantity', d.adjusted_qty); $checkbox.attr('data-quantity', d.adjusted_qty); } // 更新库存属性 if (d.max_stock) { $input.attr('max', d.max_stock); $input.attr('data-stock', d.max_stock); } // 显示通知 if (showNotice && d.notice) { alert(d.notice); } // 更新摘要 updateSelectedSummary(); } }).always(function() { $input.prop('disabled', false); $row.find('.plus, .minus').prop('disabled', false); }); } // === 处理库存超量 === function handleOverstock($input) { const maxStock = parseInt($input.attr('data-stock')) || 0; const currentQty = parseInt($input.val()) || 0; if (maxStock > 0 && currentQty > maxStock) { $input.val(currentQty); alert(`此商品库存仅剩 ${maxStock}`); $input.val(maxStock); saveQuantityAjax($input, false); return true; } return false; } // === 心愿单功能 === function handleWishlistClick(e) { e.preventDefault(); const $btn = $(this); const productId = $btn.data('product-id'); const isInWishlist = $btn.hasClass('in-wishlist'); if (isInWishlist) { // 前往心愿单页面 window.location.href = 'https://animangabookstore.com/wishlist/'; } else { // 添加到心愿单 $btn.find('.heart-icon').css('opacity', '0.5'); $.post(getAjaxUrl(), { action: 'add_to_wishlist', security: getCartNonce(), product_id: productId }, function(response) { if (response.success) { $btn.addClass('in-wishlist') .attr('title', '浏览心愿单') .find('.heart-icon') .css('fill', 'red') .css('opacity', '1'); // 更新图标样式 $btn.find('.heart-icon').css({ 'fill': 'red', 'opacity': '1' }); } else { alert(response.data.message || '添加到心愿单失败'); $btn.find('.heart-icon').css('opacity', '1'); } }).fail(function() { $btn.find('.heart-icon').css('opacity', '1'); }); } } // === 事件绑定 === function bindEvents() { // 移除默认的WooCommerce行为 $('body').off('click', '.plus, .minus'); // 数量输入 $('.woocommerce-cart-form').on('input', '.qty', function() { handleOverstock($(this)); }); // 失焦时保存 $('.woocommerce-cart-form').on('blur', '.qty', function() { saveQuantityAjax($(this), true); }); // 按回车键保存 $('.woocommerce-cart-form').on('keydown', '.qty', function(e) { if (e.key === 'Enter') { e.preventDefault(); saveQuantityAjax($(this), true); } }); // +/- 按钮,500ms延迟 $('body').on('click', '.plus,.minus', function(e) { e.stopImmediatePropagation(); const $input = $(this).closest('.quantity').find('input.qty'); const currentQty = parseInt($input.val()) || 0; const step = $(this).hasClass('plus') ? 1 : -1; const newQty = Math.max(1, currentQty + step); $input.val(newQty); if (!handleOverstock($input)) { clearTimeout($input.data('buttonSaveTimer')); $input.data('buttonSaveTimer', setTimeout(() => { saveQuantityAjax($input, true); }, 500)); } }); // 全选功能 $('#select-all-items-header, #select-all-items-footer').change(function() { const isChecked = $(this).prop('checked'); $('.item-checkbox').prop('checked', isChecked); updateSelectedSummary(); }); // 当选择改变时更新摘要 $('.item-checkbox').change(updateSelectedSummary); // 删除选中商品 $('#remove-selected-items').click(function() { const keys = $('.item-checkbox:checked').map(function() { return $(this).val(); }).get(); if (keys.length === 0) { alert('请先选择要删除的商品'); return; } $.post(getAjaxUrl(), { action: 'remove_selected_cart_items', security: getCartNonce(), cart_item_keys: keys }, function(response) { if (response.success) { keys.forEach(function(key) { $(`.item-checkbox[value="${key}"]`).closest('tr').remove(); }); updateSelectedSummary(); } }); }); // 清空购物车 $('#clear-cart').click(function() { if (!confirm('确定要清空整个购物车吗?')) return; $.post(getAjaxUrl(), { action: 'empty_cart', security: getCartNonce() }, function(response) { if (response.success) { $('.woocommerce-cart-form__cart-item').remove(); updateSelectedSummary(); } }); }); // 应用优惠券 $('#apply-coupon').click(function() { const code = $('#coupon_code').val(); if (!code) { alert('请输入优惠券代码'); return; } $.post(getAjaxUrl(), { action: 'apply_coupon', security: getCartNonce(), coupon_code: code }, function(response) { if (response.success) { alert('优惠券应用成功!'); location.reload(); } else { alert(response.data.message || '优惠券应用失败'); } }); }); // 部分结算 $('#partial-checkout').click(function(e) { const keys = $('.item-checkbox:checked').map(function() { return $(this).val(); }).get(); if (keys.length === 0) { e.preventDefault(); alert('请至少选择一件商品结算'); return false; } sessionStorage.setItem('selected_cart_items', JSON.stringify(keys)); return true; }); // 心愿单按钮点击事件 $('body').on('click', '.wishlist-icon', handleWishlistClick); } // === 初始化 === $(document).ready(function() { // 初始化库存属性 $('.item-checkbox').each(function() { const $input = $(this).closest('tr').find('.qty'); const maxStock = parseInt($input.attr('max')) || 0; if (maxStock > 0) { $input.attr('data-stock', maxStock); } }); // 初始更新摘要 updateSelectedSummary(); // 绑定所有事件 bindEvents(); }); }); </script>
without changing anything, make these code in formatted way
最新发布