52 lines
2.4 KiB
JavaScript
52 lines
2.4 KiB
JavaScript
(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); |