Searching by SKUs in the WooCommerce 2.1.x Administration
In WooCommerce 2.0.20 or less, searching products by SKU in the administration used to be done using the “SKU:” prefix, but this was removed in version 2.1. You can now search by SKU without having to add the prefix in the search form. However there’s a little bug in versions 2.1, 2.1.1 and 2.1.2 in the function that returns only one single result while it should return all products having a SKU starting by a same prefix. This should be fixed in version 2.1.3, or 2.1.4.
As a temporary fix you can add this snippet to the functions.php file in your theme folder:
<?php
/**
* Search by SKU or ID for products. Adapted from code by BenIrvin (Admin Search by ID)
*
* @access public
* @param mixed $wp
* @return void
*/
function woocommerce_admin_product_search( $wp ) {
global $pagenow, $wpdb;
if( 'edit.php' != $pagenow ) return;
if( !isset( $wp->query_vars['s'] ) ) return;
if ($wp->query_vars['post_type']!='product') return;
if( '#' == substr( $wp->query_vars['s'], 0, 1 ) ) :
$id = absint( substr( $wp->query_vars['s'], 1 ) );
if( !$id ) return;
unset( $wp->query_vars['s'] );
$wp->query_vars['p'] = $id;
elseif( 'SKU:' == strtoupper( substr( $wp->query_vars['s'], 0, 4 ) ) ) :
$sku = trim( substr( $wp->query_vars['s'], 4 ) );
if( !$sku ) return;
$ids = $wpdb->get_col( 'SELECT post_id FROM ' . $wpdb->postmeta . ' WHERE meta_key="_sku" AND meta_value LIKE "%' . $sku . '%";' );
if ( ! $ids ) return;
unset( $wp->query_vars['s'] );
$wp->query_vars['post__in'] = $ids;
$wp->query_vars['sku'] = $sku;
endif;
}
/**
* Label for the search by ID/SKU feature
*
* @access public
* @param mixed $query
* @return void
*/
function woocommerce_admin_product_search_label($query) {
global $pagenow, $typenow, $wp;
if ( 'edit.php' != $pagenow ) return $query;
if ( $typenow!='product' ) return $query;
$s = get_query_var( 's' );
if ($s) return $query;
$sku = get_query_var( 'sku' );
if($sku) {
$post_type = get_post_type_object($wp->query_vars['post_type']);
return sprintf(__( '[%s with SKU of %s]', 'woocommerce' ), $post_type->labels->singular_name, $sku);
}
$p = get_query_var( 'p' );
if ($p) {
$post_type = get_post_type_object($wp->query_vars['post_type']);
return sprintf(__( '[%s with ID of %d]', 'woocommerce' ), $post_type->labels->singular_name, $p);
}
return $query;
}
if ( is_admin() ) {
add_action( 'parse_request', 'woocommerce_admin_product_search' );
add_filter( 'get_search_query', 'woocommerce_admin_product_search_label' );
}
And then use the old search method (eg. “SKU: 7810” will return all products having the SKU starting by 7810).