Skip to content

Redirect to product page if search term exactly equals SKU or MPN #1735

@altmoola

Description

@altmoola

I came up with this solution for now by editing the SearchController, and it seems to work. I do think this should be a formal setting in the catalog/search settings. I added GetExactMatchFromSkuOrMpn to the IProductService but the name is self-explanatory. The other thing that this should do is if the SKU or MPN matches to a attribute combination of the product, it should go to that product page with the variant url.

public ActionResult Search(CatalogSearchQuery query)
{

...

try
{
    // if a search term exactly equals a SKU or MPN then redirect to that product
    // page instead of running the search
    var product = _productService.GetExactMatchFromSkuOrMpn(query.Term.Trim());
    if (product != null)
    {
        var url = Url.RouteUrl("Product", new { SeName = product.GetSeName() }, Request.Url.Scheme);

        return RedirectPermanent(url);
    }

    result = _catalogSearchService.Search(query);
}
catch (Exception ex)
{
    model.Error = ex.ToString();
    result = new CatalogSearchResult(query);
}

...

}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions