Woocommerce Admin Multiple SKU Search

Until you have used this… its hard to explain how useful this code snippet is!  Seriously!

Suppose you are given a list of SKUs and you need to bulk edit each product by adding a tag to each one.

Editing one by one could take ages.

This code enhances your woocommerce admin search bar to allow searches of multiple SKUs at once.

Copy the list of SKUs (separated by a pipe “|”) and paste them into the woocommerce product search bar.  The search will return the list of products with matching SKUs.

You can then select all of them, and then bulk edit all at once!

IMPORTANT: Don’t forget to separate the SKUs with a pipe eg 123|456|789 etc…

Pro Tip: Search for about 20 or 25 SKUs at a time.   The code itself can return more results but its the bulk edit that might grind the site to a halt.  Batches of 25 is a whole lot better that one at a time!

Enjoy!


function woo_multiple_sku_search( $query_vars ) {

global $typenow;
global $wpdb;
global $pagenow;

if ( 'product' === $typenow && isset( $_GET['s'] ) && 'edit.php' === $pagenow ) {
    $search_term = esc_sql( sanitize_text_field( $_GET['s'] ) );

    if (strpos($search_term, '|') == false) return $query_vars;

    $skus = explode('|',$search_term);

    $meta_query = array(
        'relation' => 'OR'
    );
    if(is_array($skus) && $skus) {
        foreach($skus as $sku) {
            $meta_query[] = array(
                'key' => '_sku',
                'value' => $sku,
                'compare' => '='
            );
        }
    }

    $args = array(
        'posts_per_page'  => -1,
        'post_type'       => 'product',
        'meta_query'      => $meta_query
    );
    $posts = get_posts( $args );

    if ( ! $posts ) return $query_vars;

    foreach($posts as $post){
      $query_vars['post__in'][] = $post->ID;
    }
}

return $query_vars;
}
add_filter( 'request', 'woo_multiple_sku_search', 20 );

Damon
Damon

Damon is the Principal Full Stack Developer at WPFix and freelance consultant. If you need help implementing anything wordpress related, please reach out to make an appointment.

3 Comments

  1. lon clark

    Hello,
    Please disreguard my pervious comment. I found the problem. works perfectly. Thank you so much.

    Reply
    • Damon

      Hi Ion. Pleased you got it working. Its a handy bit of code!

      Reply
  2. lon clark

    Hello, I have been trying to find a solution for this. and have tried several code snippets. none work on my site.
    WordPress 6.1.1
    woocommerce 7.1.1

    do you have any sugestions?

    Thanks for your help.

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Recent Posts

  • Setup a Wordpress Child Theme slider
  • Wordpress Better SEO GTMerix and Ligthouse Scores slider
  • Wordpress Instant Search Bar slider
  • woocommerce disable rich text editing for shop managers slider
  • Wordpress Enhanced Search slider