Home » WooCommerce: Alter Product Search @ Manual Admin Orders

WooCommerce: Alter Product Search @ Manual Admin Orders

by adminkys

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!

With the snippet below, I’ve reduced the possible search results to… 2 products only, by simply defining an array of product IDs.

PHP Snippet: Alter Product Search @ WooCommerce Order Admin

/**
 * @snippet       Only Search Array of Product IDs @ Order Admin
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 6
 * @donate $9     https://www.tutoraspire.com
 */

add_filter( 'woocommerce_product_pre_search_products', 'tutoraspire_only_search_these_products', 9999, 6 );

function tutoraspire_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;
}

You may also like