Copied to clipboard

Flag this post as spam?

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


  • Jeremias 53 posts 278 karma points
    Jan 16, 2017 @ 12:37
    Jeremias
    0

    Merchello - try to get product-groups in levels with their containing products

    Hello @all,

    I try to get the product-groups in levels with their conatining products. My idea is: I want to have all product-groups from level 1. Then i want to say: give me all child-product from each first-lefel-group. Here is a little picture, that you know what I mean with "product-groups". There you can see, what's level 1 and what's level 2 for me...!

    enter image description here

    I use the API from merchello for this. Right now I use the MerchelloHelper()

    Here is my test-code for trying to get the products:

    MerchelloHelper merchello = new MerchelloHelper();
    var allCategories = merchello.Collections.Product.GetAll();
    
    string retValue = "";
    
    foreach (IProductCollection product in allCategories)
    {
        retValue += "<br>" + product.Name + "<br>";
        IEnumerable<IProductContent> children = product.GetProducts();
        foreach (IProductContent child in children)
        {
            retValue += "-->" + child.Name + "<br>";
        }
    }
    

    My Problem is, that in this example I only get all products and not seperated in levels.

    I hope someone can help me with that!

    Thanks!

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Jan 16, 2017 @ 16:59
    Rusty Swayne
    0

    Hi Jeremias,

    You should get the root level collections (instead of all collections) and then use the tree methods ...

  • Jeremias 53 posts 278 karma points
    Jan 18, 2017 @ 08:04
    Jeremias
    0

    Hi Rusty,

    Thank you for your answer.

    MerchelloHelper merchello = new MerchelloHelper();
    var level1 = merchello.Collections.Product.GetRootLevelCollections();
    
    string retValue = "";
    
    foreach (IProductCollection level1Collection in level1)
    {
        retValue += "<br>" + level1Collection.Name + "<br>";
        var level2 = level1Collection.DescendantsOrSelf(x => x.ParentKey == level1Collection.Key);
        foreach (IProductCollection level2Collection in level2)
        {
            retValue += "-->" + level2Collection.Name + "<br>";
        }
    }
    

    This is my test-code I use now. I have only one Problem with this: I only get the Child-Elements from one of the root-Collections: enter image description here

    And here the output on the webpage:

    enter image description here

    Like you can see: Only from my root-collection "T-Shirts" I get the "children". When I only use

    var level2 = level1Collection.Descendants();
    

    I only get the children of my T-Shirt-collection for every root-collection:

    enter image description here

    I know, my output is just for testing - but I hope it shows you what I mean!

    Thanks for your help!

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Jan 23, 2017 @ 16:24
    Rusty Swayne
    100

    Hey Jeremias,

    Sorry for the delayed reply - can you check out the ftCatalog.cshtml view in the FastTrack starter and try to find a difference in what you are trying to do ... it's seems to me it is sort of the same thing ...

    Here is the view:

    https://github.com/Merchello/Merchello/blob/merchello-dev/src/Merchello.FastTrack.Ui/Views/ftCatalog.cshtml

    The model is just passed on via the Models builder, so the property editor. If you need to see how that works, it's here:

    https://github.com/Merchello/Merchello/blob/merchello-dev/src/Merchello.Web/PropertyConverters/ProductCollectionValueConverter.cs#L64

Please Sign in or register to post replies

Write your reply to:

Draft