Overview:


Enhance your WooCommerce site's search functionality with our ubie_product_search_by_sku feature. This custom function allows users to search for products using their unique SKU numbers, offering a streamlined shopping experience. Suitable for Ubie Pay Monthly Plan subscribers.


Step Guide for Ubie Implementation:


  1. Request the Feature: Email Ubie Support to request the SKU search feature for your website.
  2. Ubie Implementation: Our team will integrate the ubie_product_search_by_sku function into your website's search system.
  3. Verify Functionality: Test the search feature on your site using product SKUs.
  4. Feedback and Support: Contact Ubie Support for any issues or questions.

Step Guide for Advanced Users (Self-Implementation):


  1. Locate functions.php: Access your WordPress theme files and locate the functions.php file. This is typically found in the directory /wp-content/themes/your-theme-name/.
  2. Edit functions.php: Open functions.php in a text editor. It's recommended to back up this file before making any changes.
  3. Add the Function: Copy the following code (below) and paste it at the end of your functions.php file.
  4. Save Changes: After adding the code, save the file and upload it back to your server if you're editing it locally.
  5. Test the Functionality: Visit your website and use the search bar to find products by their SKU.
  6. Revert if Necessary: If you encounter any issues, revert to the original functions.php file from your backup.


add_filter( 'posts_search', 'ubie_product_search_by_sku', 9999, 2 );

function ubie_product_search_by_sku( $search, $wp_query ) {
   global $wpdb;
   if ( is_admin() || ! is_search() || ! isset( $wp_query->query_vars['s'] ) || ( ! is_array( $wp_query->query_vars['post_type'] ) && $wp_query->query_vars['post_type'] !== "product" ) || ( is_array( $wp_query->query_vars['post_type'] ) && ! in_array( "product", $wp_query->query_vars['post_type'] ) ) ) return $search;
   $product_id = wc_get_product_id_by_sku( $wp_query->query_vars['s'] );
   if ( ! $product_id ) return $search;
   $product = wc_get_product( $product_id );
   if ( $product->is_type( 'variation' ) ) {
      $product_id = $product->get_parent_id();
   }
   $search = str_replace( 'AND (((', "AND (({$wpdb->posts}.ID IN (" . $product_id . ")) OR ((", $search ); 
   return $search;  
}