How to add search by SKU to basic search in nopCommerce

There are many online store sites that basically manage their inventory specifically by SKUs which makes SKU (id or number) a very important aspect of their eCommerce business. Many times, online retailers like to include "search by sku" option in search on the store site that gives the ability to customers to find products by sku.

nopCommerce does allow adding "SKU" to the product catalog and when "advanced search" is enabled along with "search in product description" then only online shoppers can look for a product by SKU. 

But, there is a big possibility that many of the new online customers won't know that the search feature on the site does support "search by sku" by enabling these advanced search options. Now, this makes it very important for many store owners to add "search by sku" feature by default in the basic search itself.


Today, we will go over the process of  adding search by SKU to basic search in nopCommerce (as a default feature).


There are basically 2 methods to accomplish this.


Method 1

In your code, go to: Nop.Web / Controllers / CatalogController.cs

Open "CatalogController.cs" file

Now, look for "searchSku: searchInDescriptions" in the following section:

//products
                   products = _productService.SearchProducts(
                       categoryIds: categoryIds,
                       manufacturerId: manufacturerId,
                       storeId: _storeContext.CurrentStore.Id,
                       visibleIndividuallyOnly: true,
                       priceMin: minPriceConverted,
                       priceMax: maxPriceConverted,
                       keywords: searchTerms,
                       searchDescriptions: searchInDescriptions,
                       searchSku: searchInDescriptions,
                       searchProductTags: searchInProductTags,
                       languageId: _workContext.WorkingLanguage.Id,
                       orderBy: (ProductSortingEnum)command.OrderBy,
                       pageIndex: command.PageNumber - 1,
                       pageSize: command.PageSize);
                   model.Products = PrepareProductOverviewModels(products).ToList();


Replace "searchSku: searchInDescriptions," with "searchSku: true,"


Now, your code should look like this (as we have commented the default code):

//products
                    products = _productService.SearchProducts(
                        categoryIds: categoryIds,
                        manufacturerId: manufacturerId,
                        storeId: _storeContext.CurrentStore.Id,
                        visibleIndividuallyOnly: true,
                        priceMin: minPriceConverted,
                        priceMax: maxPriceConverted,
                        keywords: searchTerms,
                        searchDescriptions: searchInDescriptions,
                        //searchSku: searchInDescriptions,
                        searchSku: true,
                        searchProductTags: searchInProductTags,
                        languageId: _workContext.WorkingLanguage.Id,
                        orderBy: (ProductSortingEnum)command.OrderBy,
                        pageIndex: command.PageNumber - 1,
                        pageSize: command.PageSize);
                    model.Products = PrepareProductOverviewModels(products).ToList();



That's all - Compile  your solution and try searching "sku" on basic search, you should get the product(s) in the search results like this (without even going in advanced search):


nopcommerce search sku



Method 2

In your nopCommerce database, look for stored procedure "ProductLoadAllPaged"

Find this:

--SKU
        IF @SearchSku = 1
        BEGIN
            SET @sql = @sql + '
            UNION
            SELECT p.Id
            FROM Product p with (NOLOCK)
            WHERE '
            IF @UseFullTextSearch = 1
                SET @sql = @sql + 'CONTAINS(p.[Sku], @Keywords) '
            ELSE
                SET @sql = @sql + 'PATINDEX(@Keywords, p.[Sku]) > 0 '
        END


And, replace it with this:

--SKU
--IF @SearchSku = 1
--BEGIN
    SET @sql = @sql + '
    UNION
    SELECT p.Id
    FROM Product p with (NOLOCK)
    WHERE '
    IF @UseFullTextSearch = 1
        SET @sql = @sql + 'CONTAINS(p.[Sku], @Keywords) '
    ELSE
        SET @sql = @sql + 'PATINDEX(@Keywords, p.[Sku]) > 0 '
--END


Hope it helps!

About Author

Written By Lavish Kumar

Based out of New York, USA, Lavish Kumar is a full stack web developer by profession and founder of Striving Programmers, a trusted community for developers that offers a wealth of articles and forums to assist individuals with improving their software development skills.

Leave your comment