Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
altmoola opened this issue Oct 24, 2019 · 2 comments
Closed

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

altmoola opened this issue Oct 24, 2019 · 2 comments
Assignees
Labels
Milestone

Comments

@altmoola
Copy link

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);
}

...

}
@altmoola
Copy link
Author

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;
}

@muratcakir muratcakir self-assigned this Oct 25, 2019
@muratcakir muratcakir added this to the 4.0 milestone Oct 25, 2019
@muratcakir muratcakir modified the milestones: 4.0, 4.1 May 29, 2020
@muratcakir muratcakir assigned mgesing and unassigned muratcakir Jul 2, 2020
@mgesing mgesing added feature and removed review labels Jul 20, 2020
@mgesing
Copy link
Contributor

mgesing commented Jul 20, 2020

Thanks for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants