Copied to clipboard

Flag this post as spam?

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


  • Andrew Wilson 11 posts 114 karma points
    Oct 10, 2018 @ 07:41
    Andrew Wilson
    0

    Filters

    Hello

    I have used Merchello for several, smaller, projects but for this particular implementation I am wanting to filter products based on a user selection.

    I was assuming that the Specified Filters could be used for this purpose, but I am having finding any documentation on how these can be accessed in a view.

    In my example I have a single filter called "Product Type" with values such as - Table, Chair, Sofa, Armchair, Suite, Side Table, Homeware - each product can belong to multiple filters, i.e. a leather Lay-z-boy recliner can belong in the Armchar, Chair and Suite filters.

    So far I have been able to successfully add a filter group into the system (Merchello --> Products --> Filter Groups --> Add) as well as adding multiple filters to a product (Merchello --> Products --> [product] --> Product Filters -- > tick multiple filter)

    I have successfully been able to extract all of the filters which I have entered into the system using the following

    MerchelloHelper merchelloHelper = new Merchello.Web.MerchelloHelper();    
    var filterGroups = merchelloHelper.Filters.Product.GetAll();
    

    This allows me to get all of the filter groups and then iterate through them to get the Name and Key of each of the filters, but this is where I run out of information.

    What I would like to do is:

    1: Only output the filters which are used by a (list of) products

    2: Output the filters which are selected for a product

    Any help on this would be very much appreciated. I will also report back if I come across a solution during my continued investigation.

    Regards, Andrew

    EDIT: I am using v2.6.0 of Merchello

  • Andrew Wilson 11 posts 114 karma points
    Oct 10, 2018 @ 09:29
    Andrew Wilson
    102

    It's me again - talking to myself as usual.

    I did some digging and found the answers to both of my questions.

    Problem #1:

    There is a function inside of helper.Filters.Product called GetFilterGroupsForCollectionContext() which accepts a Collection Key - this will return a list of filter groups and filters, with the bonus of the occurrence of each filter being returned in the count field - this can then be used to create a dropdown or checkbox list

    var filters = merchelloHelper.Filters.Product.GetFilterGroupsForCollectionContext(Model.Content.Catalogue.CollectionKey);
    foreach (var filter in filters)
    {
        <p>@filter.Name</p>
        <ul>
            @foreach (var fil in filter.Filters)
            {
            <li>@fil.Name - <small>@fil.Key</small> - @fil.Count</li>
            }
        </ul>
    }
    

    Output: enter image description here

    Problem #2:

    Ok, I haven't figured this out, however I have found the next best thing - how to filter the products. I did this using some help from the following link (Cheers Simon and Rusty):

    https://our.umbraco.com/packages/collaboration/merchello/merchello/79449-get-list-of-products-from-collections-merchello#comment-256518

    The key is the ProductContentQuery() function inside of the Merchello helper, this allows you to chain selectors together to search and filter products. In my case I called the following to return a list of products filtered by the result of GetFilterFromSource(IProductFilter filter):

        IProductFilter testfilter = GetFilterFromSource();
        Guid collectionkey = Model.Content.Catalogue.CollectionKey;
    Merchello.Core.PagedCollection<Merchello.Web.Models.VirtualContent.IProductContent> filterQuery = merchelloHelper.ProductContentQuery().Page(1).ItemsPerPage(15).ConstrainBy(testfilter).ConstrainByCollectionKey(collectionkey).Execute();
        IEnumerable<Merchello.Web.Models.VirtualContent.IProductContent> products = filterQuery.Items;
    

    Then of course you can iterate through the products collection to output your products. The PagedCollection object also contains properties for PageSize, CurrentPage, etc.

    I am no expert on any of this so your mileage may vary. Hope it helps someone else though.

  • Jay Crowe 42 posts 173 karma points
    Nov 26, 2019 @ 20:28
    Jay Crowe
    0

    This works perfectly to apply one filter. But what if I wanted to get all of the products that were in the filter table or in chair for example?

Please Sign in or register to post replies

Write your reply to:

Draft