SKU Search for WordPress Multisite

This snippet for WordPress Multisite adds custom search functionality that allows administrators to search for products by their SKU (Stock Keeping Unit) across the entire network. This feature is particularly useful for networks that manage multiple e-commerce sites and need a centralized way to locate products based on their SKU or for those who sell the same product across multiple sites.

				
					<?php 
/**
 * Register SKU Search in network admin menu
 */
add_action('network_admin_menu', 'sku_add_to_menu');
function sku_add_to_menu() {
    add_menu_page( 
        "SKU Search", 
        "SKU Search", 
        'manage_network_options', 
        'sku_search', 
        'sku_page',
        'dashicons-search',
        5
    );  
} 

/**
 * SKU Search page
 */
function sku_page() {
    ?>
    <h2>SKU Search</h2>
    <form method="POST">
        <?php wp_nonce_field( 'sku_search_action', 'sku_search_nonce' ); ?>
        <label for="sku">SKU</label><br>
        <input type="text" id="sku" name="sku" value="<?php echo isset($_POST['sku']) ? esc_attr($_POST['sku']) : ''; ?>">
        <input type="submit" value="Search">
    </form>
    <hr>
    <?php

    if ( ! empty($_POST['sku']) && isset($_POST['sku_search_nonce']) 
         && wp_verify_nonce($_POST['sku_search_nonce'], 'sku_search_action') ) {

        $sku = sanitize_text_field($_POST['sku']);
        $blogs = get_sites();

        foreach ( $blogs as $b ) {
            switch_to_blog( $b->blog_id );

            $query = new WC_Product_Query( [
                'limit' => -1,
                'sku'   => $sku,
            ] );

            $products = $query->get_products();

            foreach ( $products as $p ) {
                $product_id   = $p->get_id();
                $product_name = $p->get_name();
                $blog_name    = get_blog_option( $b->blog_id, 'blogname' );
                $edit_url     = get_admin_url( $b->blog_id, "post.php?post={$product_id}&action=edit" );

                echo esc_html( $b->blog_id ) . ' ';
                echo '<a href="' . esc_url( $edit_url ) . '" target="_blank">' 
                        . esc_html( $blog_name . ' - ' . $product_name ) 
                     . '</a><br/>';
            }

            restore_current_blog();
        }
    }
}
?>
				
			

If this code helped you, or you have any questions on implementation, don’t hesitate to reach out.