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 5.2.0-beta.1 10.0.2 5.2.0-rc.1 10.0.3 5.2.0-rc.2 10.0.4 5.2.1 10.0.5 5.2.2 10.0.6 5.2.3 10.1.0 5.2.4 10.1.0-rc.1 5.2.5 10.1.0-rc.2 5.3.0 10.1.0-rc.3 5.3.0-beta.1 10.1.0-rc.4 5.3.0-rc.1 10.1.1 5.3.0-rc.2 10.1.2 5.3.1 10.1.3 5.3.2 10.1.4 5.3.3 10.2.0 5.4.0 10.2.0-beta.1 5.4.0-beta.1 10.2.0-beta.2 5.4.0-rc.1 10.2.0-rc.1 5.4.1 10.2.1 5.4.2 10.2.2 5.4.3 10.2.3 5.4.4 10.2.4 5.4.5 10.3.0 5.5.0 10.3.0-beta.1 5.5.0-beta.1 10.3.0-beta.2 5.5.0-rc.1 10.3.0-rc.1 5.5.0-rc.2 10.3.0-rc.2 5.5.1 10.3.1 5.5.2 10.3.2 5.5.3 10.3.3 5.5.4 10.3.4 5.5.5 10.3.5 5.6.0 10.3.6 5.6.0-beta.1 10.3.7 5.6.0-rc.1 10.3.8 5.6.0-rc.2 10.4.0 5.6.1 10.4.0-beta.1 5.6.2 10.4.0-beta.2 5.6.3 10.4.0-rc.1 5.7.0 10.4.1 5.7.0-beta.1 10.4.2 5.7.0-rc.1 10.4.3 5.7.1 10.4.4 5.7.2 10.5.0 5.7.3 10.5.0-beta.1 5.8.0 10.5.0-beta.2 5.8.0-beta.1 10.5.0-rc.1 5.8.0-beta.2 10.5.0-rc.2 5.8.0-rc.1 10.5.0-rc.3 5.8.1 10.5.1 5.8.2 10.5.2 5.9.0 10.5.3 5.9.0-beta.1 10.6.0 5.9.0-rc.1 10.6.0-beta.1 5.9.0-rc.2 10.6.0-beta.2 5.9.1 10.6.0-rc.1 5.9.2 10.6.1 6.0.0 10.6.2 6.0.0-beta.1 10.7.0 6.0.0-rc.1 10.7.0-beta.1 6.0.1 10.7.0-beta.2 6.0.2 10.7.0-rc.1 6.1.0 3.0.0 6.1.0-beta.1 3.0.1 6.1.0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7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / assets / js / admin / wc-enhanced-select.js
woocommerce / assets / js / admin Last commit date
api-keys.js 5 years ago api-keys.min.js 3 years ago backbone-modal.js 2 weeks ago backbone-modal.min.js 2 weeks ago marketplace-suggestions.js 1 year ago marketplace-suggestions.min.js 1 year ago meta-boxes-coupon.js 1 year ago meta-boxes-coupon.min.js 1 year ago meta-boxes-order.js 2 weeks ago meta-boxes-order.min.js 2 weeks ago meta-boxes-product-variation.js 2 weeks ago meta-boxes-product-variation.min.js 2 weeks ago meta-boxes-product.js 2 weeks ago meta-boxes-product.min.js 2 weeks ago meta-boxes.js 6 months ago meta-boxes.min.js 6 months ago network-orders.js 8 years ago network-orders.min.js 3 years ago order-attribution-admin.js 2 years ago order-attribution-admin.min.js 2 years ago product-editor.js 1 year ago product-editor.min.js 1 year ago product-ordering.js 5 months ago product-ordering.min.js 5 months ago quick-edit.js 2 weeks ago quick-edit.min.js 2 weeks ago reports.js 1 year ago reports.min.js 1 year ago settings-views-html-settings-tax.js 1 month ago settings-views-html-settings-tax.min.js 1 month ago settings.js 1 year ago settings.min.js 1 year ago system-status.js 1 month ago system-status.min.js 1 month ago term-ordering.js 5 months ago term-ordering.min.js 5 months ago users.js 5 years ago users.min.js 3 years ago variation-gallery.js 2 months ago variation-gallery.min.js 2 months ago wc-admin-notices.js 2 weeks ago wc-admin-notices.min.js 2 weeks ago wc-brands-enhanced-select.js 1 year ago wc-brands-enhanced-select.min.js 1 year ago wc-clipboard.js 5 years ago wc-clipboard.min.js 5 years ago wc-customer-stock-notifications.js 1 year ago wc-customer-stock-notifications.min.js 1 year ago wc-enhanced-select.js 2 weeks ago wc-enhanced-select.min.js 2 weeks ago wc-orders.js 3 weeks ago wc-orders.min.js 3 weeks ago wc-product-export.js 1 year ago wc-product-export.min.js 1 year ago wc-product-import.js 3 years ago wc-product-import.min.js 3 years ago wc-recent-reviews-widget-async.js 6 months ago wc-recent-reviews-widget-async.min.js 6 months ago wc-setup.js 5 years ago wc-setup.min.js 3 years ago wc-shipping-classes.js 1 year ago wc-shipping-classes.min.js 1 year ago wc-shipping-providers.js 5 months ago wc-shipping-providers.min.js 5 months ago wc-shipping-zone-methods.js 2 weeks ago wc-shipping-zone-methods.min.js 2 weeks ago wc-shipping-zones.js 1 year ago wc-shipping-zones.min.js 1 year ago wc-status-widget-async.js 6 months ago wc-status-widget-async.min.js 6 months ago wc-status-widget.js 1 year ago wc-status-widget.min.js 1 year ago woocommerce_admin.js 1 month ago woocommerce_admin.min.js 1 month ago
wc-enhanced-select.js
744 lines
1 /*global wc_enhanced_select_params */
2 jQuery( function ( $ ) {
3 function getVisualTermSwatchStyle( visual ) {
4 if ( ! visual.type || ! visual.value ) {
5 return '';
6 }
7
8 if ( 'image' === visual.type ) {
9 return (
10 "background-image:url('" +
11 String( visual.value ).replace( /'/g, '%27' ) +
12 "')"
13 );
14 }
15
16 if ( 'color' === visual.type ) {
17 return 'background-color:' + visual.value;
18 }
19
20 return '';
21 }
22
23 function formatVisualTermOption( term ) {
24 const swatchStyle = getVisualTermSwatchStyle( term.visual );
25 const swatchClass =
26 'wc-admin-color-swatch' + ( swatchStyle ? '' : ' is-empty' );
27 const wrapper = document.createElement( 'span' );
28 wrapper.className = 'wc-admin-visual-attribute-term-option';
29
30 const swatch = document.createElement( 'span' );
31 swatch.className = swatchClass;
32 if ( swatchStyle ) {
33 swatch.setAttribute( 'style', swatchStyle );
34 }
35 swatch.setAttribute( 'aria-hidden', 'true' );
36
37 const label = document.createElement( 'span' );
38 label.className = 'wc-admin-visual-attribute-term-label';
39 label.textContent = term.text;
40
41 wrapper.appendChild( swatch );
42 wrapper.appendChild( label );
43
44 return wrapper;
45 }
46
47 function getTermDataWithVisual( $select, data ) {
48 if ( data.visual ) {
49 return data;
50 }
51
52 const $option = $select.find( 'option' ).filter( function () {
53 return $( this ).val() === String( data.id );
54 } );
55
56 if ( ! $option.length ) {
57 return data;
58 }
59
60 const visual = $option.data( 'visual' );
61
62 if ( ! visual ) {
63 return data;
64 }
65
66 return Object.assign( {}, data, { visual: visual } );
67 }
68
69 function updateVisualAttributeTermChoices( $select ) {
70 var $container = $select.next( '.select2-container' );
71
72 if ( ! $container.length ) {
73 return;
74 }
75
76 $container.find( '.select2-selection__choice' ).each( function () {
77 const $choice = $( this );
78 const data = $choice.data( 'data' );
79
80 if ( ! data ) {
81 return;
82 }
83
84 const $removeButton = $choice
85 .find( '.select2-selection__choice__remove' )
86 .detach();
87 const termData = getTermDataWithVisual( $select, data );
88
89 $choice
90 .empty()
91 .append( $removeButton )
92 .append( formatVisualTermOption( termData ) );
93 $choice.prop( 'title', termData.title || termData.text );
94 } );
95 }
96
97 function getEnhancedSelectFormatString() {
98 return {
99 language: {
100 errorLoading: function () {
101 // Workaround for https://github.com/select2/select2/issues/4355 instead of i18n_ajax_error.
102 return wc_enhanced_select_params.i18n_searching;
103 },
104 inputTooLong: function ( args ) {
105 var overChars = args.input.length - args.maximum;
106
107 if ( 1 === overChars ) {
108 return wc_enhanced_select_params.i18n_input_too_long_1;
109 }
110
111 return wc_enhanced_select_params.i18n_input_too_long_n.replace(
112 '%qty%',
113 overChars
114 );
115 },
116 inputTooShort: function ( args ) {
117 var remainingChars = args.minimum - args.input.length;
118
119 if ( 1 === remainingChars ) {
120 return wc_enhanced_select_params.i18n_input_too_short_1;
121 }
122
123 return wc_enhanced_select_params.i18n_input_too_short_n.replace(
124 '%qty%',
125 remainingChars
126 );
127 },
128 loadingMore: function () {
129 return wc_enhanced_select_params.i18n_load_more;
130 },
131 maximumSelected: function ( args ) {
132 if ( args.maximum === 1 ) {
133 return wc_enhanced_select_params.i18n_selection_too_long_1;
134 }
135
136 return wc_enhanced_select_params.i18n_selection_too_long_n.replace(
137 '%qty%',
138 args.maximum
139 );
140 },
141 noResults: function () {
142 return wc_enhanced_select_params.i18n_no_matches;
143 },
144 searching: function () {
145 return wc_enhanced_select_params.i18n_searching;
146 },
147 },
148 };
149 }
150
151 try {
152 $( document.body )
153 .on( 'wc-enhanced-select-init', function () {
154 // Regular select boxes
155 $( ':input.wc-enhanced-select, :input.chosen_select' )
156 .filter( ':not(.enhanced)' )
157 .each( function () {
158 var select2_args = $.extend(
159 {
160 minimumResultsForSearch: 10,
161 allowClear: $( this ).data( 'allow_clear' )
162 ? true
163 : false,
164 placeholder: $( this ).data( 'placeholder' ),
165 },
166 getEnhancedSelectFormatString()
167 );
168
169 $( this )
170 .selectWoo( select2_args )
171 .addClass( 'enhanced' );
172 } );
173
174 $(
175 ':input.wc-enhanced-select-nostd, :input.chosen_select_nostd'
176 )
177 .filter( ':not(.enhanced)' )
178 .each( function () {
179 var select2_args = $.extend(
180 {
181 minimumResultsForSearch: 10,
182 allowClear: true,
183 placeholder: $( this ).data( 'placeholder' ),
184 },
185 getEnhancedSelectFormatString()
186 );
187
188 $( this )
189 .selectWoo( select2_args )
190 .addClass( 'enhanced' );
191 } );
192
193 function display_result( self, select2_args ) {
194 select2_args = $.extend(
195 select2_args,
196 getEnhancedSelectFormatString()
197 );
198
199 $( self ).selectWoo( select2_args ).addClass( 'enhanced' );
200
201 if ( $( self ).data( 'sortable' ) ) {
202 var $select = $( self );
203 var $list = $( self )
204 .next( '.select2-container' )
205 .find( 'ul.select2-selection__rendered' );
206
207 $list.sortable( {
208 placeholder:
209 'ui-state-highlight select2-selection__choice',
210 forcePlaceholderSize: true,
211 items: 'li:not(.select2-search__field)',
212 tolerance: 'pointer',
213 stop: function () {
214 $(
215 $list
216 .find( '.select2-selection__choice' )
217 .get()
218 .reverse()
219 ).each( function () {
220 var id = $( this ).data( 'data' ).id;
221 var option = $select.find(
222 'option[value="' + id + '"]'
223 )[ 0 ];
224 $select.prepend( option );
225 } );
226 },
227 } );
228 // Keep multiselects ordered alphabetically if they are not sortable.
229 } else if ( $( self ).prop( 'multiple' ) ) {
230 $( self ).on( 'change', function () {
231 var $children = $( self ).children();
232 $children.sort( function ( a, b ) {
233 var atext = a.text.toLowerCase();
234 var btext = b.text.toLowerCase();
235
236 if ( atext > btext ) {
237 return 1;
238 }
239 if ( atext < btext ) {
240 return -1;
241 }
242 return 0;
243 } );
244 $( self ).html( $children );
245 } );
246 }
247 }
248
249 // Ajax product search box
250 $( ':input.wc-product-search' )
251 .filter( ':not(.enhanced)' )
252 .each( function () {
253 var select2_args = {
254 allowClear: $( this ).data( 'allow_clear' )
255 ? true
256 : false,
257 placeholder: $( this ).data( 'placeholder' ),
258 minimumInputLength: $( this ).data(
259 'minimum_input_length'
260 )
261 ? $( this ).data( 'minimum_input_length' )
262 : '3',
263 escapeMarkup: function ( m ) {
264 return m;
265 },
266 ajax: {
267 url: wc_enhanced_select_params.ajax_url,
268 dataType: 'json',
269 delay: 250,
270 data: function ( params ) {
271 let exclude = $( this ).data( 'exclude' );
272 const currentValue = $( this ).val();
273
274 // Hide already selected options from the results in multiple selects.
275 if (
276 Array.isArray( currentValue ) &&
277 $( this ).prop( 'multiple' )
278 ) {
279 const defaultExcluded = exclude
280 ? String( exclude ).split( ',' )
281 : [];
282 exclude = [
283 ...defaultExcluded,
284 ...currentValue,
285 ];
286 }
287
288 return {
289 term: params.term,
290 action:
291 $( this ).data( 'action' ) ||
292 'woocommerce_json_search_products_and_variations',
293 security:
294 wc_enhanced_select_params.search_products_nonce,
295 exclude: exclude,
296 exclude_type:
297 $( this ).data( 'exclude_type' ),
298 include: $( this ).data( 'include' ),
299 limit: $( this ).data( 'limit' ),
300 display_stock:
301 $( this ).data( 'display_stock' ),
302 };
303 },
304 processResults: function ( data ) {
305 var terms = [];
306 if ( data ) {
307 $.each( data, function ( id, text ) {
308 terms.push( {
309 id: id,
310 text: text,
311 } );
312 } );
313 }
314 return {
315 results: terms,
316 };
317 },
318 cache: true,
319 },
320 };
321
322 display_result( this, select2_args );
323 } );
324
325 // Ajax Page Search.
326 $( ':input.wc-page-search' )
327 .filter( ':not(.enhanced)' )
328 .each( function () {
329 var select2_args = {
330 allowClear: $( this ).data( 'allow_clear' )
331 ? true
332 : false,
333 placeholder: $( this ).data( 'placeholder' ),
334 minimumInputLength: $( this ).data(
335 'minimum_input_length'
336 )
337 ? $( this ).data( 'minimum_input_length' )
338 : '3',
339 escapeMarkup: function ( m ) {
340 return m;
341 },
342 ajax: {
343 url: wc_enhanced_select_params.ajax_url,
344 dataType: 'json',
345 delay: 250,
346 data: function ( params ) {
347 return {
348 term: params.term,
349 action:
350 $( this ).data( 'action' ) ||
351 'woocommerce_json_search_pages',
352 security:
353 wc_enhanced_select_params.search_pages_nonce,
354 exclude: $( this ).data( 'exclude' ),
355 post_status:
356 $( this ).data( 'post_status' ),
357 limit: $( this ).data( 'limit' ),
358 };
359 },
360 processResults: function ( data ) {
361 var terms = [];
362 if ( data ) {
363 $.each( data, function ( id, text ) {
364 terms.push( {
365 id: id,
366 text: text,
367 } );
368 } );
369 }
370 return {
371 results: terms,
372 };
373 },
374 cache: true,
375 },
376 };
377
378 $( this )
379 .selectWoo( select2_args )
380 .addClass( 'enhanced' );
381 } );
382
383 // Ajax customer search boxes
384 $( ':input.wc-customer-search' )
385 .filter( ':not(.enhanced)' )
386 .each( function () {
387 var select2_args = {
388 allowClear: $( this ).data( 'allow_clear' )
389 ? true
390 : false,
391 placeholder: $( this ).data( 'placeholder' ),
392 minimumInputLength: $( this ).data(
393 'minimum_input_length'
394 )
395 ? $( this ).data( 'minimum_input_length' )
396 : '1',
397 escapeMarkup: function ( m ) {
398 return m;
399 },
400 ajax: {
401 url: wc_enhanced_select_params.ajax_url,
402 dataType: 'json',
403 delay: 1000,
404 data: function ( params ) {
405 return {
406 term: params.term,
407 action: 'woocommerce_json_search_customers',
408 security:
409 wc_enhanced_select_params.search_customers_nonce,
410 exclude: $( this ).data( 'exclude' ),
411 };
412 },
413 processResults: function ( data ) {
414 var terms = [];
415 if ( data ) {
416 $.each( data, function ( id, text ) {
417 terms.push( {
418 id: id,
419 text: text,
420 } );
421 } );
422 }
423 return {
424 results: terms,
425 };
426 },
427 cache: true,
428 },
429 };
430
431 select2_args = $.extend(
432 select2_args,
433 getEnhancedSelectFormatString()
434 );
435
436 $( this )
437 .selectWoo( select2_args )
438 .addClass( 'enhanced' );
439
440 if ( $( this ).data( 'sortable' ) ) {
441 var $select = $( this );
442 var $list = $( this )
443 .next( '.select2-container' )
444 .find( 'ul.select2-selection__rendered' );
445
446 $list.sortable( {
447 placeholder:
448 'ui-state-highlight select2-selection__choice',
449 forcePlaceholderSize: true,
450 items: 'li:not(.select2-search__field)',
451 tolerance: 'pointer',
452 stop: function () {
453 $(
454 $list
455 .find(
456 '.select2-selection__choice'
457 )
458 .get()
459 .reverse()
460 ).each( function () {
461 var id = $( this ).data( 'data' ).id;
462 var option = $select.find(
463 'option[value="' + id + '"]'
464 )[ 0 ];
465 $select.prepend( option );
466 } );
467 },
468 } );
469 }
470 } );
471
472 // Ajax category search boxes
473 $( ':input.wc-category-search' )
474 .filter( ':not(.enhanced)' )
475 .each( function () {
476 var return_format = $( this ).data( 'return_id' )
477 ? 'id'
478 : 'slug';
479
480 var select2_args = $.extend(
481 {
482 allowClear: $( this ).data( 'allow_clear' )
483 ? true
484 : false,
485 placeholder: $( this ).data( 'placeholder' ),
486 minimumInputLength: $( this ).data(
487 'minimum_input_length'
488 )
489 ? $( this ).data( 'minimum_input_length' )
490 : '3',
491 escapeMarkup: function ( m ) {
492 return m;
493 },
494 ajax: {
495 url: wc_enhanced_select_params.ajax_url,
496 dataType: 'json',
497 delay: 250,
498 data: function ( params ) {
499 return {
500 term: params.term,
501 action: 'woocommerce_json_search_categories',
502 security:
503 wc_enhanced_select_params.search_categories_nonce,
504 };
505 },
506 processResults: function ( data ) {
507 var terms = [];
508 if ( data ) {
509 $.each(
510 data,
511 function ( id, term ) {
512 terms.push( {
513 id:
514 'id' ===
515 return_format
516 ? term.term_id
517 : term.slug,
518 text: term.formatted_name,
519 } );
520 }
521 );
522 }
523 return {
524 results: terms,
525 };
526 },
527 cache: true,
528 },
529 },
530 getEnhancedSelectFormatString()
531 );
532
533 $( this )
534 .selectWoo( select2_args )
535 .addClass( 'enhanced' );
536 } );
537
538 // Ajax category search boxes
539 $( ':input.wc-taxonomy-term-search' )
540 .filter( ':not(.enhanced)' )
541 .each( function () {
542 var return_format = $( this ).data( 'return_id' )
543 ? 'id'
544 : 'slug';
545 var isVisualAttribute =
546 'yes' === $( this ).data( 'isVisualAttribute' );
547
548 var select2_args = $.extend(
549 {
550 allowClear: $( this ).data( 'allow_clear' )
551 ? true
552 : false,
553 placeholder: $( this ).data( 'placeholder' ),
554 minimumInputLength:
555 $( this ).data( 'minimum_input_length' ) !==
556 null &&
557 $( this ).data( 'minimum_input_length' ) !==
558 undefined
559 ? $( this ).data(
560 'minimum_input_length'
561 )
562 : '3',
563 escapeMarkup: function ( m ) {
564 return m;
565 },
566 ajax: {
567 url: wc_enhanced_select_params.ajax_url,
568 dataType: 'json',
569 delay: 250,
570 data: function ( params ) {
571 return {
572 taxonomy:
573 $( this ).data( 'taxonomy' ),
574 limit: $( this ).data( 'limit' ),
575 orderby:
576 $( this ).data( 'orderby' ),
577 term: params.term,
578 action: 'woocommerce_json_search_taxonomy_terms',
579 security:
580 wc_enhanced_select_params.search_taxonomy_terms_nonce,
581 };
582 },
583 processResults: function ( data ) {
584 var terms = [];
585 if ( data ) {
586 $.each(
587 data,
588 function ( id, term ) {
589 var termData = {
590 id:
591 'id' ===
592 return_format
593 ? term.term_id
594 : term.slug,
595 text: term.name,
596 };
597
598 if ( isVisualAttribute ) {
599 termData.visual =
600 term.visual || {
601 type: 'none',
602 value: '',
603 };
604 }
605
606 terms.push( termData );
607 }
608 );
609 }
610 return {
611 results: terms,
612 };
613 },
614 cache: true,
615 },
616 },
617 getEnhancedSelectFormatString()
618 );
619
620 if ( isVisualAttribute ) {
621 select2_args.templateResult = function ( term ) {
622 if ( term.loading ) {
623 return term.text;
624 }
625
626 return formatVisualTermOption( term );
627 };
628 }
629
630 var $select = $( this );
631
632 $select
633 .selectWoo( select2_args )
634 .addClass( 'enhanced' );
635
636 if ( isVisualAttribute ) {
637 updateVisualAttributeTermChoices( $select );
638
639 $select.on(
640 'change.wcVisualAttributeTerms select2:select.wcVisualAttributeTerms',
641 function () {
642 updateVisualAttributeTermChoices( $select );
643 }
644 );
645 }
646 } );
647
648 $( ':input.wc-attribute-search' )
649 .filter( ':not(.enhanced)' )
650 .each( function () {
651 var select2Element = this;
652 var select2_args = $.extend(
653 {
654 allowClear: $( this ).data( 'allow_clear' )
655 ? true
656 : false,
657 placeholder: $( this ).data( 'placeholder' ),
658 minimumInputLength:
659 $( this ).data( 'minimum_input_length' ) !==
660 null &&
661 $( this ).data( 'minimum_input_length' ) !==
662 undefined
663 ? $( this ).data(
664 'minimum_input_length'
665 )
666 : '3',
667 escapeMarkup: function ( m ) {
668 return m;
669 },
670 ajax: {
671 url: wc_enhanced_select_params.ajax_url,
672 dataType: 'json',
673 delay: 250,
674 data: function ( params ) {
675 return {
676 term: params.term,
677 action: 'woocommerce_json_search_product_attributes',
678 security:
679 wc_enhanced_select_params.search_product_attributes_nonce,
680 };
681 },
682 processResults: function ( data ) {
683 var disabledItems =
684 $( select2Element ).data(
685 'disabled-items'
686 ) || [];
687 var terms = [];
688 if ( data ) {
689 $.each(
690 data,
691 function ( id, term ) {
692 terms.push( {
693 id: term.slug,
694 text: term.name,
695 disabled:
696 disabledItems.includes(
697 term.slug
698 ),
699 } );
700 }
701 );
702 }
703 return {
704 results: terms,
705 };
706 },
707 cache: true,
708 },
709 },
710 getEnhancedSelectFormatString()
711 );
712
713 $( this )
714 .selectWoo( select2_args )
715 .addClass( 'enhanced' );
716 } );
717 } )
718
719 // WooCommerce Backbone Modal
720 .on( 'wc_backbone_modal_before_remove', function () {
721 $(
722 '.wc-enhanced-select, :input.wc-product-search, :input.wc-customer-search'
723 )
724 .filter( '.select2-hidden-accessible' )
725 .selectWoo( 'close' );
726 } )
727
728 .trigger( 'wc-enhanced-select-init' );
729
730 $( 'html' ).on( 'click', function ( event ) {
731 if ( this === event.target ) {
732 $(
733 '.wc-enhanced-select, :input.wc-product-search, :input.wc-customer-search'
734 )
735 .filter( '.select2-hidden-accessible' )
736 .selectWoo( 'close' );
737 }
738 } );
739 } catch ( err ) {
740 // If select2 failed (conflict?) log the error but don't stop other scripts breaking.
741 window.console.log( err );
742 }
743 } );
744