From 26e076af1ad003d0050211612152129b0ab5799d Mon Sep 17 00:00:00 2001 From: tikkhun Date: Thu, 6 Nov 2025 15:36:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=E6=B7=B7=E8=A3=85?= =?UTF-8?q?=E4=BA=A7=E5=93=81=E5=8D=A1=E7=89=87=E5=BC=8F=E5=B8=83=E5=B1=80?= =?UTF-8?q?=E5=8F=8A=E6=89=B9=E9=87=8F=E6=B7=BB=E5=8A=A0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增卡片式布局模板替换原有表格形式 - 添加后台一键批量选择所有simple product功能 - 优化前端样式兼容性及交互体验 - 增加版本号管理及资源文件版本控制 - 重构前端逻辑提升代码可维护性 --- assets/css/frontend.css | 70 ++++++- assets/js/admin.js | 53 +++++- docs/需求.md | 1 + .../class-yoone-product-bundles-admin.php | 56 +++++- includes/class-yoone-product-bundles.php | 11 ++ .../class-yoone-product-bundles-frontend.php | 174 +++++++++++------- templates/single-product-yoone-bundle.php | 144 +++++++++++++++ .../add-to-cart/yoone-bundle.php | 125 ------------- yoone-product-bundles.php | 10 +- 9 files changed, 445 insertions(+), 199 deletions(-) create mode 100644 docs/需求.md create mode 100644 templates/single-product-yoone-bundle.php delete mode 100644 templates/single-product/add-to-cart/yoone-bundle.php diff --git a/assets/css/frontend.css b/assets/css/frontend.css index 8522df1..ae4374c 100644 --- a/assets/css/frontend.css +++ b/assets/css/frontend.css @@ -5,4 +5,72 @@ .yoone-bundle-table { width: 100%; border-collapse: collapse; } .yoone-bundle-table th, .yoone-bundle-table td { border-bottom: 1px solid #eee; padding: 8px; } .yoone-bundle-meta { display: flex; gap: 1em; align-items: center; } -.yoone-bundle-actions { margin-top: 1em; } \ No newline at end of file +.yoone-bundle-actions { margin-top: 1em; } + +/* 在混装产品页面隐藏默认产品图片区域(兼容常见主题结构) */ +.product.type-yoone_bundle div.images, +.single-product .product.type-yoone_bundle .images { + display: none !important; +} +/* 兼容通过 body_class 添加的标记 */ +.yoone-bundle-product div.images { display: none !important; } + +/* 表格列宽优化 */ +.yoone-bundle-table th:nth-child(3), +.yoone-bundle-table td:nth-child(3) { width: 160px; } + +/* 强制混装产品页面的内容区域为全宽,以容纳卡片布局 */ +.yoone-bundle-product-page .summary { + width: 100% !important; /* 覆盖主题默认的宽度限制 */ + float: none !important; /* 清除浮动 */ +} + +.yoone-bundle-add-to-cart-button { + margin-top: 1em; +} + +/* 新的卡片式布局样式 */ +.yoone-bundle-items-wrapper { + display: flex; + flex-wrap: wrap; + gap: 20px; /* 卡片之间的间距 */ +} + +.yoone-bundle-item-card { + display: flex; + flex-direction: column; + align-items: center; /* 垂直居中对齐 */ + width: 150px; /* 卡片宽度,可根据需要调整 */ + border: 1px solid #eee; + border-radius: 8px; + padding: 10px; + text-align: center; + transition: box-shadow 0.3s; +} + +.yoone-bundle-item-card:hover { + box-shadow: 0 4px 8px rgba(0,0,0,0.1); +} + +.yoone-bundle-item-card .item-image img { + max-width: 100%; + height: auto; + border-radius: 4px; +} + +.yoone-bundle-item-card .item-title { + font-size: 14px; + margin: 8px 0; + font-weight: bold; +} + +.yoone-bundle-item-card .item-price { + font-size: 16px; + color: #c00; + margin-bottom: 8px; +} + +.yoone-bundle-item-card .item-quantity input { + width: 80px; + text-align: center; +} \ No newline at end of file diff --git a/assets/js/admin.js b/assets/js/admin.js index 94aa59f..2563f26 100644 --- a/assets/js/admin.js +++ b/assets/js/admin.js @@ -1,3 +1,52 @@ -(function($){ - // 预留:后台增强脚本(例如联动过滤 simple products、校验最小数量等)。 +(function($) { + $(document).ready(function() { + // “一键添加所有 Simple Product” 按钮点击事件 + $(document).on('click', '.yoone-add-all-simple-products', function(e) { + e.preventDefault(); + + var $button = $(this); + var $select = $('select.wc-product-search[name="yoone_bundle_allowed_products[]"]'); + var nonce = $('#yoone_bundle_admin_nonce_field').val(); + + $button.prop('disabled', true).text('正在加载...'); + + $.ajax({ + url: ajaxurl, + method: 'POST', + data: { + action: 'yoone_get_all_simple_products', + security: nonce + }, + success: function(response) { + if (response.success) { + var existing_ids = $select.val() || []; + var products = response.data; + + products.forEach(function(product) { + // 如果下拉列表中不存在该选项,则添加 + if ($select.find('option[value="' + product.id + '"]').length === 0) { + var newOption = new Option(product.text, product.id, false, false); + $select.append(newOption); + } + // 将新获取的商品ID加入到已选中的ID数组中 + if (existing_ids.indexOf(product.id.toString()) === -1) { + existing_ids.push(product.id.toString()); + } + }); + + // 更新 select2 的选中状态 + $select.val(existing_ids).trigger('change'); + $button.prop('disabled', false).text('一键添加所有 Simple Product'); + } else { + alert('加载失败,请重试。'); + $button.prop('disabled', false).text('一键添加所有 Simple Product'); + } + }, + error: function() { + alert('请求失败,请检查网络连接或联系管理员。'); + $button.prop('disabled', false).text('一键添加所有 Simple Product'); + } + }); + }); + }); })(jQuery); \ No newline at end of file diff --git a/docs/需求.md b/docs/需求.md new file mode 100644 index 0000000..294d919 --- /dev/null +++ b/docs/需求.md @@ -0,0 +1 @@ +- 混装产品可批量添加simple product为混装可选商品 \ No newline at end of file diff --git a/includes/admin/class-yoone-product-bundles-admin.php b/includes/admin/class-yoone-product-bundles-admin.php index cf9402e..8ba9389 100644 --- a/includes/admin/class-yoone-product-bundles-admin.php +++ b/includes/admin/class-yoone-product-bundles-admin.php @@ -19,6 +19,11 @@ class Yoone_Product_Bundles_Admin { add_action('woocommerce_product_data_panels', array($this, 'render_product_data_panel')); // 保存配置 add_action('woocommerce_admin_process_product_meta', array($this, 'save_product_meta')); + add_action('woocommerce_process_product_meta', array($this, 'save_product_meta')); + add_action('save_post_product', array($this, 'save_product_meta'), 15); + + // AJAX: 获取所有 simple product + add_action('wp_ajax_yoone_get_all_simple_products', array($this, 'ajax_get_all_simple_products')); } public function add_product_data_tab($tabs) { @@ -39,13 +44,17 @@ class Yoone_Product_Bundles_Admin { $min_qty = $config['min_qty']; $cats = $config['categories']; - echo '