Copied to clipboard

Flag this post as spam?

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


These support forums are now closed for new topics and comments.
Please head on over to http://eureka.ucommerce.net/ for support.

  • Graham Carr 277 posts 389 karma points
    Sep 12, 2011 @ 20:55
    Graham Carr
    0

    Performing product based searches

    I have a requirements to search on products based on their custom properties. The search will be similar to sites such as ASOS.com with a side bar allowing you to select from drop-downs and checkboxes to filter your products.

    I have got some way of performing this as follows:

    @{var q = from product in Product.All()
    where product.ProductProperties.Where(property =>
    (property.ProductDefinitionField.Name == "Size" && property.Value ==
    Request["size"])).Count() == 1
    && product.ParentProductId == null
    select product;
    }

    @{var products = q.ToList();}

    @foreach (var product in products)

    However, the areas I am struggling with are as follows:

    1) I am still in the process of learning LINQ so I am not sure how you perform a query based on multiple custom properties of the same type (e.g. The user selects three different types of style which are passed in the query as style=Style1,Style2,Style3)

    2) How do you build up a query based on what the user has selected. E.g. the user has selected a size from a dropdown, and a style and a price. I tried the following but it's not quite right as it assumes the custom property will always exist.

    @{var q = from product in Product.All()
    where product.ProductProperties.Where(property =>
    (property.ProductDefinitionField.Name == "Size" && property.Value
    == Request["size"])
    && (property.ProductDefinitionField.Name == "Stone" && property.Value
    == Request["stone"])
    && (property.ProductDefinitionField.Name == "Colour" && property.Value
    == Request["colour"])).Count() == 1
    && product.ParentProductId == null
    select product;
    }

    @{var products = q.ToList();}

    @foreach (var product in products)

    Any help on this, or pointing me in the right direction would be great.

     

  • Graham Carr 277 posts 389 karma points
    Sep 13, 2011 @ 11:52
    Graham Carr
    0

    Managed to solve this myself, so for the benefit of others out there, this is how I performed the filtering of products based on the passed in query string via AJAX.

    @using UCommerce.EntitiesV2
    @using UCommerce.Runtime
    @using UCommerce.Xslt

    @{
        @* Initialise local variables *@
        int i = 0;
        string catalog = SiteContext.Current.CatalogContext.CurrentCatalogName;
      
        @* Select all products as default *@
        var q = from product in Product.All() where product.ParentProductId == null
          select product;
      
        @* Build the search query *@
        if(String.IsNullOrEmpty(Request["size"]) == false)
        {
          q = from product in q where product.ProductProperties.Where(property =>
              (property.ProductDefinitionField.Name == "Size" && property.Value == Request["size"])).Count() == 1
            select product;
        }
        if(String.IsNullOrEmpty(Request["style"]) == false)
        {
          string[] styles = Request["style"].Split(',');
          q = from product in q where product.ProductProperties.Where(property =>
              (property.ProductDefinitionField.Name == "Style")
              && (styles.Contains(property.Value))).Count() == 1
            select product;
        }   
        if(String.IsNullOrEmpty(Request["stone"]) == false)
        {
          q = from product in q where product.ProductProperties.Where(property =>
              (property.ProductDefinitionField.Name == "Stone" && property.Value.Contains(Request["stone"]))).Count() == 1
            select product;
        }
        if(String.IsNullOrEmpty(Request["colour"]) == false)
        {
          q = from product in q where product.ProductProperties.Where(property =>
              (property.ProductDefinitionField.Name == "Colour" && property.Value == Request["colour"])).Count() == 1
            select product;
        }
        if(String.IsNullOrEmpty(Request["finish"]) == false)
        {
          string[] finishes = Request["finish"].Split(',');
          q = from product in q where product.ProductProperties.Where(property =>
              (property.ProductDefinitionField.Name == "Finish")
              && (finishes .Contains(property.Value))).Count() == 1
            select product;
        }   
        if(String.IsNullOrEmpty(Request["position"]) == false)
        {
          string[] positions= Request["position"].Split(',');
          q = from product in q where product.ProductProperties.Where(property =>
              (property.ProductDefinitionField.Name == "Position")
              && (positions.Contains(property.Value))).Count() == 1
            select product;
        }   
        
        @* Return the products as a list *@
        var products = q.ToList();
      }

    @* Loop through the products and output the html *@
    @foreach (var product in products)
    {
     i++;
     <text>Output your product specific HTML here</text>
    }
  • Søren Spelling Lund 1797 posts 2786 karma points
    Sep 13, 2011 @ 15:54
    Søren Spelling Lund
    0

    Nicely done. Thanks for sharing.

Please Sign in or register to post replies

Write your reply to:

Draft