WooCommerce Search by SKU

Add WooCommerce Product By SKU Programmatically

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

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. Install WP fastest site search plugin from WordPress plugin store.
  2. Go to https://cse.expertrec.com/newuser?platform=wordpress.
  3. Enter your WordPress website URL and click on next->
  4. The crawl will start in the background.
  5. Login to your WordPress admin panel.
  6. In your left panel you must be able to see expertrec’s WordPress plugin, click on that.
  7. Get your site ID (or) API key from your expertrec control panel ( here ).
  8. Copy paste your site id in your WordPress control panel and click on save changes.
  9. Make sure you have upgraded to a paid plan before 15 days of free trial. (Price begins at 9 USD per month)
  10. 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

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published.

You may also like