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).

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):

 

 Looking for quality nopCommerce Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , | Posted under nopCommerce | RSS 2.0

Author Spotlight

Lavish Kumar

Founder, developer and owner of StrivingProgrammers.com - By profession, he is a software programmer / web developer, web designer & DBA. He is passionate about technology and electronic gadgets. He enjoys writing tech articles and developing web applications. He is a nopCommerce MVP and author of official nopCommerce Designer's Guide.

Leave a Reply

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