skip to Main Content

How to Enable Search by Product SKU in WooCommerce

How to Enable Search by Product SKU in WooCommerce

Overview

If you run a WooCommerce store, improving search functionality can significantly enhance the user experience. One effective method is allowing customers to search for products using their SKUs (Stock Keeping Units). In this blog post, we’ll guide you through enabling SKU-based searches in WooCommerce using a custom code snippet.


What is SKU?

SKU stands for Stock Keeping Unit. It is a unique identifier for each product, allowing for efficient tracking and inventory management. By enabling SKU searches, customers can quickly locate specific products, leading to a smoother shopping experience.


Why Enable SKU Search?

Improved User Experience
Allowing customers to search by SKU simplifies their shopping journey. If they know the SKU, they can quickly find the item without scrolling through numerous options. This convenience enhances customer satisfaction and loyalty.

Increased Sales
When customers can easily locate products, they are more likely to complete their purchases. Efficient product discovery reduces friction in the buying process, which can lead to higher conversion rates and increased sales.

Better Inventory Management
SKU searches help store owners gain insights into inventory levels and customer preferences. By tracking frequently searched SKUs, you can manage stock levels effectively and make informed decisions about restocking and promotions.

Enhanced Customer Support
With SKU search functionality, customer support teams can quickly assist customers looking for specific items. This efficiency reduces response times and enhances the overall service experience.

Competitive Advantage
Offering advanced search capabilities, such as SKU searches, can differentiate your WooCommerce store from competitors. It shows a commitment to customer satisfaction and can attract more shoppers.


Steps to Enable SKU Search

Step 1: Backup Your Site
Before making any changes, always back up your website. This ensures you can restore it if something goes wrong.

Step 2: Add the Custom Code
You’ll need to add a custom function to your theme’s functions.php file or create a site-specific plugin. Here’s the code to enable SKU search:

add_filter( 'pre_get_posts', 'codebykp_sku_search_helper' );

function codebykp_sku_search_helper( $wp ) {
    global $wpdb;
    $uwa_table_postmeta = $wpdb->prefix . 'postmeta';

    // Check to see if query is requested
    if ( ! isset( $wp->query['s'] ) || ! isset( $wp->query['post_type'] ) || $wp->query['post_type'] != 'product' ) {
        return;
    }
    
    $sku = $wp->query['s'];

    // Construct cache key
    $cache_key = 'sku_ids_' . $sku;
    $ids       = wp_cache_get( $cache_key, 'postmeta' );

    if ( false === $ids ) {
        // Execute query to fetch product IDs by SKU
        $ids = $wpdb->get_col( $wpdb->prepare("SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = '_sku' AND meta_value = %s", $sku) );

        // Cache the IDs
        if ( $ids ) {
            wp_cache_set( $cache_key, $ids, 'postmeta' );
        }
    }

    if ( ! $ids ) {
        return;
    }
    
    // Clear the search query
    unset( $wp->query['s'] );
    unset( $wp->query_vars['s'] );
    
    // Add product IDs to the query
    $wp->query['post__in'] = array();
    foreach ( $ids as $id ) {
        $post = get_post( $id );
        if ( $post->post_type == 'product_variation' ) {
            $wp->query['post__in'][]      = $post->post_parent;
            $wp->query_vars['post__in'][] = $post->post_parent;
        } else {
            $wp->query_vars['post__in'][] = $post->ID;
        }
    }
}

Step 3: Save and Test the Search Functionality

After adding the code, save the changes to your functions.php file. Then, go to your WooCommerce store and use the search bar to enter a SKU. Verify that the products associated with that SKU appear in the search results.


Conclusion

Enabling SKU searches in WooCommerce is a straightforward yet effective way to enhance your store’s search functionality. By implementing the provided code snippet, you help customers find products faster, improving their overall shopping experience. Feel free to customize the code to meet your specific needs.


FAQ

What is a SKU?

A SKU (Stock Keeping Unit) is a unique identifier assigned to each product, which helps in tracking inventory and sales.

How does enabling SKU search benefit my store?

Enabling SKU search improves user experience, increases sales, enhances inventory management, and offers better customer support.

Can I customize the code provided?

Yes, you can modify the code to better fit your store’s specific requirements.

Where do I add the code?

You can add the code to your theme’s functions.php file or create a site-specific plugin.

What should I do if something goes wrong?

Always back up your site before making changes. If issues arise, you can restore your site from the backup.


I’m a WordPress developer with 10+ years of experience in WooCommerce and custom plugins. I combine technical expertise with design flair to help you create standout, user-friendly websites. Let’s transform your digital presence!

This Post Has 0 Comments

Leave a Reply

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

Back To Top
Search