admin.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * StudioU WC Mandatory Products Admin JavaScript
  3. */
  4. jQuery(function($) {
  5. 'use strict';
  6. // Initialize WooCommerce product search on existing elements
  7. function initProductSearch() {
  8. $('.wc-product-search').each(function() {
  9. if ($(this).data('select2')) {
  10. // Already initialized
  11. return;
  12. }
  13. $(this).selectWoo({
  14. allowClear: $(this).data('allow_clear') ? true : false,
  15. placeholder: $(this).data('placeholder'),
  16. minimumInputLength: 1,
  17. escapeMarkup: function(m) {
  18. return m;
  19. },
  20. ajax: {
  21. url: wc_enhanced_select_params.ajax_url,
  22. dataType: 'json',
  23. delay: 250,
  24. data: function(params) {
  25. return {
  26. term: params.term,
  27. action: $(this).data('action') || 'woocommerce_json_search_products_and_variations',
  28. security: wc_enhanced_select_params.search_products_nonce,
  29. exclude: $(this).data('exclude'),
  30. include: $(this).data('include'),
  31. limit: $(this).data('limit')
  32. };
  33. },
  34. processResults: function(data) {
  35. var terms = [];
  36. if (data) {
  37. $.each(data, function(id, text) {
  38. terms.push({id: id, text: text});
  39. });
  40. }
  41. return {
  42. results: terms
  43. };
  44. },
  45. cache: true
  46. }
  47. });
  48. });
  49. }
  50. // Initialize product search for existing fields
  51. $(document).ready(function() {
  52. initProductSearch();
  53. });
  54. // Add new mandatory product row
  55. $(document).on('click', '.add-mandatory-product', function() {
  56. var template = wp.template('mandatory-product-row');
  57. $('#studiou_mandatory_products_container').append(template());
  58. // Initialize product search for the new row
  59. initProductSearch();
  60. });
  61. // Remove mandatory product row
  62. $(document).on('click', '.remove-mandatory-product', function() {
  63. $(this).closest('.mandatory-product-row').remove();
  64. });
  65. });