/** * StudioU WC Mandatory Products Admin JavaScript */ jQuery(function($) { 'use strict'; // Initialize WooCommerce product search on existing elements function initProductSearch() { $('.wc-product-search').each(function() { if ($(this).data('select2')) { // Already initialized return; } $(this).selectWoo({ allowClear: $(this).data('allow_clear') ? true : false, placeholder: $(this).data('placeholder'), minimumInputLength: 1, escapeMarkup: function(m) { return m; }, ajax: { url: wc_enhanced_select_params.ajax_url, dataType: 'json', delay: 250, data: function(params) { return { term: params.term, action: $(this).data('action') || 'woocommerce_json_search_products_and_variations', security: wc_enhanced_select_params.search_products_nonce, exclude: $(this).data('exclude'), include: $(this).data('include'), limit: $(this).data('limit') }; }, processResults: function(data) { var terms = []; if (data) { $.each(data, function(id, text) { terms.push({id: id, text: text}); }); } return { results: terms }; }, cache: true } }); }); } // Initialize product search for existing fields $(document).ready(function() { initProductSearch(); }); // Add new mandatory product row $(document).on('click', '.add-mandatory-product', function() { var template = wp.template('mandatory-product-row'); $('#studiou_mandatory_products_container').append(template()); // Initialize product search for the new row initProductSearch(); }); // Remove mandatory product row $(document).on('click', '.remove-mandatory-product', function() { $(this).closest('.mandatory-product-row').remove(); }); });