Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Harsheet 71 posts 302 karma points
    Feb 23, 2017 @ 22:35
    Harsheet
    0

    Ucommerce product search

    Hey guys,

    I have to implement uCommerce product search on title and description.

    Any help regarding this?

  • Paul Seal 524 posts 2890 karma points MVP 7x c-trib
    Feb 24, 2017 @ 16:05
    Paul Seal
    102

    I used this:

    IEnumerable<Product> products = new List<Product>();
    
    if (!string.IsNullOrWhiteSpace(keyword))
    {
        products = Product.Find(p => 
            p.VariantSku == null && p.DisplayOnSite &&
            (
                p.Sku.Contains(keyword)
                || p.Name.Contains(keyword)
                || p.ProductDescriptions.Any(
                    d => d.DisplayName.Contains(keyword) 
                    || d.ShortDescription.Contains(keyword) 
                    || d.LongDescription.Contains(keyword)
                )
            )
        );
    }
    
  • Harsheet 71 posts 302 karma points
    Feb 26, 2017 @ 22:00
    Harsheet
    0

    Thank you so much Paul. This works!

  • Paul Seal 524 posts 2890 karma points MVP 7x c-trib
    Feb 26, 2017 @ 22:17
    Paul Seal
    0

    Great news. 👍🏻

  • Harsheet 71 posts 302 karma points
    Mar 16, 2017 @ 23:27
    Harsheet
    0

    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

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

    Can you help?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies