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.
- Deactivate the plugin used for this.
- Open search.php in your theme.
- After the heading, we will add our code to get the results.
- First of all, we will take the search query and find out the product id which has this SKU.
- 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.
- Then we will query the product with this product id.
Below is the code, how we can achieve this.
Leave a Reply