65
Exactly. By default, when you search for orders in the WordPress > WooCommerce > Orders backend, using a SKU will give you 0 results. Which is pretty poor for a store manager.
Let’s change this. Despite the code is a little complex, all you need is a simple copy & paste. Enjoy!
PHP Snippet: Allow Search By SKU @ WooCommerce “Orders” Admin Page
/**
* @snippet Search By SKU @ Orders Dashboard - WooCommerce
* @how-to Get tutoraspire.com FREE
* @author Tutor Aspire
* @compatible WooCommerce 6
* @donate $9 https://tutoraspire.com
*/
add_filter( 'woocommerce_shop_order_search_results', 'tutoraspire_order_search_by_sku', 9999, 3 );
function tutoraspire_order_search_by_sku( $order_ids, $term, $search_fields ) {
global $wpdb;
if ( ! empty( $search_fields ) ) {
$product_id = wc_get_product_id_by_sku( $wpdb->esc_like( wc_clean( $term ) ) );
if ( ! $product_id ) return $order_ids;
$order_ids = array_unique(
$wpdb->get_col(
$wpdb->prepare( "SELECT order_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key IN ( '_product_id', '_variation_id' ) AND meta_value = %d ) AND order_item_type = 'line_item'", $product_id )
)
);
}
return $order_ids;
}
Once the snippet is active, and for the same search query as per the previous screenshot, here’s the result: