I want to remove Diacritics from the searched keyword and also from name and displayname of the product in order to match them perfectly.
For eg. If I search for L’Oréal, it gives me correct results, but if I search for Loreal, nothing comes up.
Here is the code
string whatToSearch = "L'oreal";
whatToSearch = whatToSearch.RemoveDiacritics();
var products = new List<UCommerce.EntitiesV2.Product>();
if (!string.IsNullOrWhiteSpace(whatToSearch))
{
products = UCommerce.EntitiesV2.Product.Find(p =>
p.VariantSku == null && p.DisplayOnSite &&
(
p.Sku.Contains(whatToSearch)
|| p.Name.RemoveDiacritics().Contains(whatToSearch)
|| p.ProductDescriptions.Any(
d => d.DisplayName.RemoveDiacritics().Contains(whatToSearch)
|| d.ShortDescription.Contains(whatToSearch)
|| d.LongDescription.Contains(whatToSearch)
)
)
);
}
var productIds = products.Select(x => x.Id).ToList();
And here is the extension method I am using to remove the accents
public static string RemoveDiacritics(this string s)
{
var normalizedString = s.Normalize(NormalizationForm.FormD);
var stringBuilder = new StringBuilder();
foreach (var c in normalizedString)
{
if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
stringBuilder.Append(c);
}
return stringBuilder.ToString();
}
Ucommerce product search
Hey guys,
I have to implement uCommerce product search on title and description.
Any help regarding this?
I used this:
Thank you so much Paul. This works!
Great news. 👍🏻
Hey Paul,
Another question from this.
I want to remove Diacritics from the searched keyword and also from name and displayname of the product in order to match them perfectly.
For eg. If I search for L’Oréal, it gives me correct results, but if I search for Loreal, nothing comes up.
Here is the code
And here is the extension method I am using to remove the accents
Can you help?
is working on a reply...