WooCommerce Search by SKU

Add WooCommerce Product By SKU Programmatically

Rate this article

Share this article

WooCommerce is an eCommerce platform, you will have to search for products in different ways and one of them is using SKU.

 SKU stands for Stock Keep Unit which is used to track and provide products using its SKU id.

To equip SKU search in a WooCommerce site, you will need to append ‘SKU:’ at the start of your product search followed by the product ID in your product’s panel of WooCommerce.

This search will return you the product whose product ID you have specified.

SKU in WooCommerce

To perform an SKU search on the site programmatically,  you can use the below-shown function.

function get_product_by_sku( $sku ) {
  global $wpdb;
  $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
  if ( $product_id ) return new WC_Product( $product_id );
  return null;
}

This function takes the SKU PRODUCT ID  as an argument and performs an SQL Query search with the product id you provided and returns the result which is found.

SKU Product ID SQL QUERY

Add WooCommerce Product Search by SKU

An Easier Way to add WooCommerce Product Search by SKU is by using WP fastest site search plugin by Expertrec

  1. Login to your wordpress admin panel.
  2. In the left panel click on plugins->add new plugin and search for wp fastest site search.
  3. Install the plugin and activate it.
  4. After activating the plugin you willbe navigated to signup page. Signup is a two step process. In step1 you need to enter site url and choose indexing option (if woocommerce is not active).Click on continue for step2.
  5. In the step2 you will have 2 options . Either signup through google or you can signup through otp. Choose an option and complete signup.
  6. Make sure you have upgraded to a paid plan before 15 days of free trial. (Price begins at 9 USD per month)
  7. For a detailed explaination regarding our plugin integration, features and how it will help in your search you can refer this blog  Wordpress fastest site search
  8. That’s it! Your brand new WordPress search is up and running!

Get WOOCOMMERCE Search for your Website at $9

 

Here is more information on appending SKU to the products in a website

YouTube video

Are you showing the right products, to the right shoppers, at the right time? Contact us to know more.
You may also like