Woocommerce search by SKU programmatically without plugin

Posted on by Chandan

“Search by SKU” is very common in websites with woocommerce and have a large product base. There are some plugins available that enable SKU searching and show the result in the search results page.

But these available plugins fail to show the resulting product which has “SKU with spaces”. For example “AS 011 022 ZE” will result in nothing. 

So, to overcome these problems with those plugins, we can modify the search.php page inside the active theme. In this article, we will do this programmatically.

 

 

The basic idea behind our modifications is to show the SKU result first.

  1. Deactivate the plugin used for this.
  2. Open search.php in your theme.
  3. After the heading, we will add our code to get the results.
  4. First of all, we will take the search query and find out the product id which has this SKU.
  5. We will show this result at the top, so we will also get the paged variable and if it is the first page then we will show the results.
  6. Then we will query the product with this product id.

 

Below is the code, how we can achieve this.

About Chandan

Author View all posts by Chandan →

Leave a Reply

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

*

*