85 lines
2.9 KiB
JavaScript
85 lines
2.9 KiB
JavaScript
/* Yoone Product Bundles - Admin dynamic grouping terms
|
|
* When switching grouping taxonomy (category/tag), refresh the terms select list accordingly.
|
|
*/
|
|
(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[]"]');
|
|
|
|
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);
|
|
});
|
|
|
|
// Add all simple products to the products select
|
|
if ($addAllBtn.length && $productSelect.length) {
|
|
$addAllBtn.on('click', function(){
|
|
var $btn = $(this);
|
|
$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');
|
|
});
|
|
}
|
|
});
|
|
})(jQuery); |