diff --git a/assets/js/admin/wc-enhanced-select.js b/assets/js/admin/wc-enhanced-select.js index 5438085a90d..4eac44f6371 100644 --- a/assets/js/admin/wc-enhanced-select.js +++ b/assets/js/admin/wc-enhanced-select.js @@ -91,6 +91,7 @@ jQuery( function( $ ) { action : $( this ).data( 'action' ) || 'woocommerce_json_search_products_and_variations', security : wc_enhanced_select_params.search_products_nonce, exclude : $( this ).data( 'exclude' ), + exclude_type : $( this ).data( 'exclude_type' ), include : $( this ).data( 'include' ), limit : $( this ).data( 'limit' ), display_stock: $( this ).data( 'display_stock' ) @@ -260,14 +261,16 @@ jQuery( function( $ ) { // WooCommerce Backbone Modal .on( 'wc_backbone_modal_before_remove', function() { - $( '.wc-enhanced-select, :input.wc-product-search, :input.wc-customer-search' ).filter( '.select2-hidden-accessible' ).selectWoo( 'close' ); + $( '.wc-enhanced-select, :input.wc-product-search, :input.wc-customer-search' ).filter( '.select2-hidden-accessible' ) + .selectWoo( 'close' ); }) .trigger( 'wc-enhanced-select-init' ); $( 'html' ).on( 'click', function( event ) { if ( this === event.target ) { - $( '.wc-enhanced-select, :input.wc-product-search, :input.wc-customer-search' ).filter( '.select2-hidden-accessible' ).selectWoo( 'close' ); + $( '.wc-enhanced-select, :input.wc-product-search, :input.wc-customer-search' ).filter( '.select2-hidden-accessible' ) + .selectWoo( 'close' ); } } ); } catch( err ) { diff --git a/includes/class-wc-ajax.php b/includes/class-wc-ajax.php index 48a7eef227b..d1e3b534d26 100644 --- a/includes/class-wc-ajax.php +++ b/includes/class-wc-ajax.php @@ -1540,6 +1540,15 @@ class WC_AJAX { $include_ids = ! empty( $_GET['include'] ) ? array_map( 'absint', (array) wp_unslash( $_GET['include'] ) ) : array(); $exclude_ids = ! empty( $_GET['exclude'] ) ? array_map( 'absint', (array) wp_unslash( $_GET['exclude'] ) ) : array(); + if ( ! empty( $_GET['exclude_type'] ) ) { + $exclude_types = array_map( 'strtolower', (array) wp_unslash( $_GET['exclude_type'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized + + // Sanitize the excluded types against valid product types. + $exclude_types = array_intersect( array_keys( wc_get_product_types() ), $exclude_types ); + } else { + $exclude_types = array(); + } + $data_store = WC_Data_Store::load( 'product' ); $ids = $data_store->search_products( $term, '', (bool) $include_variations, false, $limit, $include_ids, $exclude_ids ); @@ -1550,6 +1559,10 @@ class WC_AJAX { $formatted_name = $product_object->get_formatted_name(); $managing_stock = $product_object->managing_stock(); + if ( in_array( $product_object->get_type(), $exclude_types, true ) ) { + continue; + } + if ( $managing_stock && ! empty( $_GET['display_stock'] ) ) { $stock_amount = $product_object->get_stock_quantity(); /* Translators: %d stock amount */