You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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);
}
...
}
The text was updated successfully, but these errors were encountered:
I tweaked it to redirect to pre-selected variants as well:
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
ProductVariantAttributeCombination variant;
var product = _productService.GetExactMatchFromSkuOrMpn(query.Term.Trim(), out variant);
if (product != null)
{
string url = null;
if (variant != null)
url = _productUrlHelper.GetProductUrl(product.Id, product.GetSeName(), variant.AttributesXml);
else
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);
}
...
}
Also since it's a little bit more complicated, here is the GetExactMatchFromSkuOrMpn method:
public Product GetExactMatchFromSkuOrMpn(string term, out ProductVariantAttributeCombination variant)
{
variant = null;
if (string.IsNullOrEmpty(term))
return null;
term = term.Trim();
var query = from p in _productRepository.Table
where !p.Deleted && p.Published && (p.Sku == term || p.ManufacturerPartNumber == term)
select p;
var product = query.FirstOrDefault();
if (product != null)
return product;
var query2 = from pva in _productVariantAttributeCombinationRepository.Table
where !pva.Product.Deleted && pva.Product.Published && (pva.Sku == term || pva.ManufacturerPartNumber == term)
select pva;
var pvaResult = query2.FirstOrDefault();
if (pvaResult != null)
variant = pvaResult;
product = pvaResult?.Product;
return product;
}
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 addedGetExactMatchFromSkuOrMpn
to theIProductService
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.The text was updated successfully, but these errors were encountered: