Ok, we’re in a niche of a niche this time – manual orders (orders created by the WooCommerce administrator). If you’re familiar with that, the admin would click on “Add order“, fill out the billing & shipping information, and then move to the order items section, where they can add products to the order.
As soon as they click on “Add items” > “Add products“, a table displays with a product search and quantity input. That’s exactly where we’re working today: what if you have 10,000 products in your store, but only create manual invoices with the same 2-3 products? In this case scenario, it makes no sense to search for the whole 10,000 product list and wait for WooCommerce to return a result (slowly) – it’s much more efficient to reduce that list to a specific category or a list of IDs so that the search operations can be faster.
Here’s how it’s done. Enjoy!
PHP Snippet: Alter Product Search @ WooCommerce Order Admin
/**
* @snippet Only Search Array of Product IDs @ Order Admin
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @testedwith WooCommerce 6
* @community https://businessbloomer.com/club/
*/
add_filter( 'woocommerce_product_pre_search_products', 'bbloomer_only_search_these_products', 9999, 6 );
function bbloomer_only_search_these_products( $custom_query, $term, $type, $include_variations, $all_statuses, $limit ) {
if ( ! isset( $_GET['exclude_type'] ) ) return $custom_query;
// we should be on the order admin page now
$product_ids = array( 21230, 21231 );
return $product_ids;
}