Search Woocommerce Orders by SKU script
a client wanted to search orders by SKU – which woocomemrce does not do!. luckily though there were a few old plugins that might have done this (but they had not been updated for a long time) i found this script – whcih worked straight away. Dropped this in my child theme function file and …. there we go
/**
* @snippet Search By SKU @ Orders Dashboard – WooCommerce
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @compatible WooCommerce 6
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/
add_filter( ‘woocommerce_shop_order_search_results’, ‘bbloomer_order_search_by_sku’, 9999, 3 );
function bbloomer_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;
}
Thanks to this for sorting me out: https://www.businessbloomer.com/woocommerce-search-orders-by-sku/ defintely deserve $9 tip
