Learn how to fix Divi footer not showing or updating issues in WordPress. Easy steps to troubleshoot and solve footer problems fast.
How to Enable Search by Product SKU in WooCommerce

Table of Contents
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
- Letting customers search by SKU simplifies their shopping journey.
- Enhances customer satisfaction and loyalty.
- Increased Sales
- Easier product discovery reduces friction in the buying process.
- Boosts conversion rates and sales.
- Better Inventory Management
- Tracks frequently searched SKUs for optimized stock levels.
- Helps make data-driven restocking decisions.
- Enhanced Customer Support
- SKU search enables faster customer assistance.
- Reduces response times and improves service quality.
- Competitive Advantage
- Advanced search capabilities set your WooCommerce store apart.
- Attracts more customers with improved functionality.
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.
This Post Has 0 Comments