175 lines
6.5 KiB
JavaScript
175 lines
6.5 KiB
JavaScript
/* Yoone Product Bundles - Admin dynamic grouping terms
|
|
* When switching grouping taxonomy (category/tag), refresh the terms select list accordingly.
|
|
*/
|
|
// Wrap ALL logic inside the jQuery IIFE to keep variables scoped and avoid ReferenceErrors
|
|
(function($){
|
|
$(function(){
|
|
var $tax = $('#yoone_bundle_group_taxonomy');
|
|
var $terms = $('#yoone_bundle_group_terms');
|
|
var $addAllBtn = $('.yoone-add-all-simple-products');
|
|
var $clearBtn = $('.yoone-clear-products-list');
|
|
var $productSelect = $('select[name="yoone_bundle_allowed_products[]"]');
|
|
var $selectMode = $('#yoone_bundle_select_mode');
|
|
var $productsField = $('#yoone_bundle_products_list_field');
|
|
|
|
if (typeof YoonePBAdmin === 'undefined') {
|
|
return;
|
|
}
|
|
|
|
function refreshTerms(tax) {
|
|
if (!tax) return;
|
|
$terms.prop('disabled', true);
|
|
$.post(YoonePBAdmin.ajax_url, {
|
|
action: 'yoone_get_taxonomy_terms',
|
|
taxonomy: tax,
|
|
security: YoonePBAdmin.security
|
|
}).done(function(resp){
|
|
if (resp && resp.success && Array.isArray(resp.data)) {
|
|
var items = resp.data;
|
|
$terms.empty();
|
|
items.forEach(function(item){
|
|
var opt = $('<option/>').val(item.id).text(item.text);
|
|
$terms.append(opt);
|
|
});
|
|
// re-init enhanced selects
|
|
$(document.body).trigger('wc-enhanced-select-init');
|
|
}
|
|
}).always(function(){
|
|
$terms.prop('disabled', false);
|
|
});
|
|
}
|
|
|
|
$tax.on('change', function(){
|
|
var val = $(this).val();
|
|
refreshTerms(val);
|
|
});
|
|
|
|
function updateProductsListState() {
|
|
var mode = $selectMode.val();
|
|
var isAll = (mode === 'all');
|
|
// 在 All 模式下,隐藏整个选择框字段;其它模式显示并更新标签文案
|
|
if ($productsField.length) {
|
|
$productsField.toggle(!isAll);
|
|
var $label = $productsField.find('> label');
|
|
if ($label.length) {
|
|
$label.text(mode === 'exclude' ? 'Excluded Products' : 'Included Products');
|
|
}
|
|
var $desc = $productsField.find('.yoone-bundle-list-desc');
|
|
if ($desc.length) {
|
|
if (mode === 'exclude') {
|
|
$desc.text('Simple products only. In Exclude mode, ALL published simple products are available EXCEPT the listed ones.');
|
|
} else {
|
|
$desc.text('Simple products only. In Include mode, ONLY the listed products will be available.');
|
|
}
|
|
}
|
|
}
|
|
// 额外:在 All 模式下禁用按钮与选择框(避免误操作)
|
|
$productSelect.prop('disabled', isAll);
|
|
$addAllBtn.prop('disabled', isAll);
|
|
}
|
|
|
|
// Add all simple products to the products select
|
|
if ($addAllBtn.length && $productSelect.length) {
|
|
$addAllBtn.on('click', function(){
|
|
var $btn = $(this);
|
|
var mode = $selectMode.val();
|
|
if (mode === 'exclude') {
|
|
// 在排除模式下“添加全部”会导致有效商品为空,先弹出确认。
|
|
var ok = window.confirm('You are in Exclude mode. Adding ALL simple products to the exclusion list will result in NO effective products. Continue?');
|
|
if (!ok) return;
|
|
}
|
|
$btn.prop('disabled', true);
|
|
$productSelect.prop('disabled', true);
|
|
$.post(YoonePBAdmin.ajax_url, {
|
|
action: 'yoone_get_all_simple_products',
|
|
security: YoonePBAdmin.security
|
|
}).done(function(resp){
|
|
if (resp && resp.success && Array.isArray(resp.data)) {
|
|
// Clear then add all as selected
|
|
$productSelect.empty();
|
|
resp.data.forEach(function(item){
|
|
var existing = $productSelect.find('option[value="'+item.id+'"]');
|
|
if (!existing.length) {
|
|
var opt = $('<option/>').val(item.id).text(item.text).prop('selected', true);
|
|
$productSelect.append(opt);
|
|
} else {
|
|
existing.prop('selected', true);
|
|
}
|
|
});
|
|
// Trigger change for select2/Woo product search
|
|
$productSelect.trigger('change');
|
|
}
|
|
}).always(function(){
|
|
$btn.prop('disabled', false);
|
|
$productSelect.prop('disabled', false);
|
|
});
|
|
});
|
|
}
|
|
|
|
// Clear products list quickly
|
|
if ($clearBtn.length && $productSelect.length) {
|
|
$clearBtn.on('click', function(){
|
|
$productSelect.find('option').prop('selected', false);
|
|
// For select2/Woo product search, simply trigger change after clearing selection
|
|
$productSelect.val([]).trigger('change');
|
|
});
|
|
}
|
|
|
|
function renderEffectiveList(items, count) {
|
|
var $list = $('#yoone_effective_products_list');
|
|
var $count = $('.yoone-effective-count');
|
|
if (!$list.length || !$count.length) return;
|
|
$count.text(count || 0);
|
|
var html = '';
|
|
if (items && items.length) {
|
|
html += '<ul class="yoone-effective-list" style="max-height:220px; overflow:auto; margin:0; padding-left:1.5em; border:1px solid #ddd; background:#fff;">';
|
|
items.forEach(function(item){
|
|
html += '<li>' + $('<div/>').text(item.text).html() + ' <span style="color:#999;">(#' + item.id + ')</span></li>';
|
|
});
|
|
html += '</ul>';
|
|
} else {
|
|
html += '<p class="description">No products will be available with the current configuration.</p>';
|
|
}
|
|
$list.html(html);
|
|
}
|
|
|
|
function recomputeEffective() {
|
|
if (typeof YoonePBAdmin === 'undefined') return;
|
|
var mode = $selectMode.val();
|
|
var allowed = $productSelect.val() || [];
|
|
$.post(YoonePBAdmin.ajax_url, {
|
|
action: 'yoone_calc_effective_products',
|
|
security: YoonePBAdmin.security,
|
|
select_mode: mode,
|
|
allowed_products: allowed
|
|
}).done(function(resp){
|
|
if (resp && resp.success && resp.data) {
|
|
renderEffectiveList(resp.data.items || [], resp.data.count || 0);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 初始化:根据当前模式更新显示并首次计算有效列表
|
|
updateProductsListState();
|
|
recomputeEffective();
|
|
|
|
// 事件:模式切换与列表变更时重新计算
|
|
if ($selectMode.length) {
|
|
$selectMode.on('change', function(){
|
|
updateProductsListState();
|
|
recomputeEffective();
|
|
});
|
|
}
|
|
if ($productSelect.length) {
|
|
$productSelect.on('change', recomputeEffective);
|
|
}
|
|
|
|
// 刷新按钮:手动触发重新计算
|
|
var $refresh = $('.yoone-refresh-effective-products');
|
|
if ($refresh.length) {
|
|
$refresh.on('click', function(){
|
|
recomputeEffective();
|
|
});
|
|
}
|
|
});
|
|
})(jQuery); |